当前位置:   article > 正文

使用Tensorflow + JupyterNotebook构建简单的神经网络_用jupyter notebook跑的神经网络模型在哪

用jupyter notebook跑的神经网络模型在哪
  1. import numpy as np
  2. import tensorflow as tf
  3. import matplotlib as plt
  4. def addlayer(inputs, insize, outsize, activefunction=None):
  5. Weight = tf.Variable(tf.random_normal([insize, outsize]))
  6. biases = tf.Variable(tf.zeros([1, outsize]) + 0.1)
  7. Wx_plus_b = tf.matmul(inputs,Weight) + biases
  8. if activefunction is None:
  9. outputs = Wx_plus_b
  10. else:
  11. outputs = activefunction(Wx_plus_b)
  12. return outputs
  13. x_data = np.linspace(-1,1,300)[:, np.newaxis]
  14. nosic = np.random.normal(0, 0.05, x_data.shape)
  15. y_data = np.square(x_data)- 0.5 + nosic
  16. xs = tf.placeholder(tf.float32,[None,1])
  17. ys = tf.placeholder(tf.float32,[None,1])
  18. l1 = addlayer(xs, 1, 10, activefunction=tf.nn.relu)
  19. prediction = addlayer(l1,10,1,activefunction=None)
  20. loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
  21. train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
  22. init = tf.initialize_all_variables()
  23. sess = tf.Session()
  24. sess.run(init)
  25. fig = plt.figure()
  26. ax = fig.add_subplot(1,1,1)
  27. ax.scatter(x_data,y_data)
  28. plt.ion()
  29. plt.show()
  30. for i in range(1000):
  31. sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
  32. if i % 50 == 0:
  33. # print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
  34. try:
  35. ax.lines.remove(lines[0])
  36. except Exception:
  37. pass
  38. prediction_value = sess.run(prediction,feed_dict={xs:x_data})
  39. lines = ax.plot(x_data,prediction_value,'r-',lw=5)
  40. plt.pause(0.1)

这样直接在JupyterNotebook上运行会报错:NameError:name 'time' is not defined!

针对这个错误只需要在文件代码开始的位置加一句代码:%matplotlib即可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/711280
推荐阅读
相关标签
  

闽ICP备14008679号