莫烦TensorFlow_05 add_layer
2024-09-07 13:13:45
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}
)
)
最新文章
- iOS之UITableView组头组尾视图/标题悬停
- 用css计算选中的复选框有几个
- POJ-1182 分组并查集
- PYTHON开发--面向对象基础二
- Java位运算在程序设计中的使用:位掩码(BitMask)
- 你好,C++(35)类是如何藏私房钱的?6.2.4 拷贝构造函数
- 【整理】01. jQuery.Form.js 用法分析
- Python3 读写文件
- Spark Streaming实战演练
- ubuntu16.04x下搜狗输入法无法输入中文
- (O)阻止默认事件和阻止冒泡的应用场景
- linux内核完全剖析——基于0.12内核-笔记(2)-统一编址和独立编址
- 为K8S集群建立只读权限帐号
- Python写随机发红包的原理流程
- 【Web Shell】- 技术剖析中国菜刀 - Part II
- JDBC处理文本和二进制文件
- 嵌入式C语言自我修养 08:变参函数的格式检查
- angular关于表单指令的汇总
- 流畅的python 闭包
- 剑指offer--28.栈的压入、弹出序列
热门文章
- MySQL学习笔记5——编码
- Python process (进程)
- QAxBase: Error calling IDispatch member LineStyle: Unknown error
- Spring Boot(十二):LocalDateTime格式化处理
- EF直接更新数据(不需查询)
- 有关tab页的
- 优雅的解决springboot Aop @Cacheable this不生效
- LeetCode 133:克隆图 Clone Graph
- js生成条形码
- 微软宣布成立.NET基金会全面支持开源项目 包括C#编译器Roslyn【转】