Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import numpy as np
- import tensorflow as tf
- import tqdm
- import matplotlib.pyplot as plt
- %matplotlib inline
- import seaborn
- from tensorflow.python.framework import ops
- ops.reset_default_graph()
- sess =tf.InteractiveSession()
- # создадим выборку
- x = np.linspace(0,10, 1000)
- y = np.sin(x) + np.random.normal(size=len(x))
- plt.plot(x,y)
- plt.show()
- # и разобьем её на тренировочную и контрольную части
- train_idxes = np.random.choice(list(range(len(x))), 3 * len(x)//4)
- test_idxes = np.array(range(len(x)))
- test_idxes = np.delete(test_idxes, train_idxes)
- X_Train = x[train_idxes]
- Y_Train = y[train_idxes]
- X_Test = x[test_idxes]
- Y_Test = y[test_idxes]
- #Создадим граф
- x_ = tf.placeholder(name="input", shape=[None, 1], dtype=tf.float32)
- y_ = tf.placeholder(name= "output", shape=[None, 1], dtype=tf.float32)
- model_output = tf.Variable(tf.random_normal([1]), name='bias') + tf.Variable(tf.random_normal([1]), name='k') * x_ # k*x+b
- loss = tf.reduce_mean(tf.pow(y_ - model_output, 2)) # функция потерь
- gd = tf.train.GradientDescentOptimizer(0.0001) #оптимизатор
- train_step = gd.minimize(loss)
- sess.run(tf.global_variables_initializer())
- n_epochs = 100
- train_errors = []
- test_errors = []
- for i in tqdm.tqdm(range(n_epochs)): # 100
- _, train_err = sess.run([train_step, loss ], feed_dict={x_:X_Train.reshape((len(X_Train),1)) , y_: Y_Train.reshape((len(Y_Train),1))})
- train_errors.append(train_err)
- test_errors.append(sess.run(loss, feed_dict={x_:X_Test.reshape((len(X_Test),1)) , y_: Y_Test.reshape((len(Y_Test),1))}))
- plt.plot(list(range(n_epochs)), train_errors, label = 'train' )
- plt.plot(list(range(n_epochs)), test_errors, label='test')
- plt.legend()
- plt.savefig('lin_reg.png')
- print(train_errors[:10])
- print(test_errors[:10])
- plt.show()
- plt.plot(x, y)
- plt.plot(x,sess.run(model_output, feed_dict={x_:x.reshape((len(x),1))}))
- plt.savefig("lr_forward_pass.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement