import tensorflow as tf
import numpy as np def add_layer(inputs, in_size, out_size, activation_function = None):
Weights = tf.Variable(tf.random_normal([in_size, out_size])) # hang lie
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs x_data = np.linspace(-1,1,300)[:, np.newaxis] # 一列;[np.newaxis,:] 一行
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise #input layer 1
#hidden layer 10
#output layer 1 xs = tf.placeholder(tf.float32, [None, 1]) # 行数不固定,列数是1
ys = tf.placeholder(tf.float32, [None, 1]) l1 = add_layer(xs, 1, 10, activation_function = tf.nn.relu)
prediction = add_layer(l1, 10, 1, activation_function = None) loss = tf.reduce_mean(
tf.reduce_sum(
tf.square(ys - prediction),
reduction_indices=[1]
)
) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init) for i in range(1000):
sess.run(train_step, feed_dict={xs:x_data, ys:y_data})
if i % 50 == 0:
print(sess.run(loss,
feed_dict={xs:x_data, ys:y_data}
)
)

  

最新文章

  1. iOS之UITableView组头组尾视图/标题悬停
  2. 用css计算选中的复选框有几个
  3. POJ-1182 分组并查集
  4. PYTHON开发--面向对象基础二
  5. Java位运算在程序设计中的使用:位掩码(BitMask)
  6. 你好,C++(35)类是如何藏私房钱的?6.2.4 拷贝构造函数
  7. 【整理】01. jQuery.Form.js 用法分析
  8. Python3 读写文件
  9. Spark Streaming实战演练
  10. ubuntu16.04x下搜狗输入法无法输入中文
  11. (O)阻止默认事件和阻止冒泡的应用场景
  12. linux内核完全剖析——基于0.12内核-笔记(2)-统一编址和独立编址
  13. 为K8S集群建立只读权限帐号
  14. Python写随机发红包的原理流程
  15. 【Web Shell】- 技术剖析中国菜刀 - Part II
  16. JDBC处理文本和二进制文件
  17. 嵌入式C语言自我修养 08:变参函数的格式检查
  18. angular关于表单指令的汇总
  19. 流畅的python 闭包
  20. 剑指offer--28.栈的压入、弹出序列

热门文章

  1. MySQL学习笔记5——编码
  2. Python process (进程)
  3. QAxBase: Error calling IDispatch member LineStyle: Unknown error
  4. Spring Boot(十二):LocalDateTime格式化处理
  5. EF直接更新数据(不需查询)
  6. 有关tab页的
  7. 优雅的解决springboot Aop @Cacheable this不生效
  8. LeetCode 133:克隆图 Clone Graph
  9. js生成条形码
  10. 微软宣布成立.NET基金会全面支持开源项目 包括C#编译器Roslyn【转】