1 Star 1 Fork 1

海之号角/TensorFlowDemo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tf_demo07_build_neural_network.py 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
海之号角 提交于 2016-12-11 09:56 +08:00 . 提交项目文件
# coding:utf-8
"""
tf.demo07_build_neural_network
Created on 2016/12/9 14:45
@author: GuoYufu
@group : OceanHorn
@contact: OceanHorn@163.com
"""
from tf_demo06_define_addLayer import *
import numpy as np
if __name__ == "__main__":
x_data = np.linspace(start=-1, stop=1, num=300)[:, np.newaxis]
noise = np.random.normal(loc=0, scale=0.05, size=x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
xs = tf.placeholder(dtype=tf.float32, shape=[None, 1])
ys = tf.placeholder(dtype=tf.float32, shape=[None, 1])
layer1_outputs = add_layer(inputs=xs, in_size=1, out_size=10, activation_function=tf.nn.relu)
prediction_layer_outputs = add_layer(inputs=layer1_outputs, in_size=10, out_size=1, activation_function=None)
loss = tf.reduce_mean(
tf.reduce_sum(
tf.square(ys - prediction_layer_outputs), reduction_indices=[1]
)
)
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)
initialize = tf.global_variables_initializer()
with tf.Session() as session:
session.run(initialize)
for i in range(1001):
session.run(train_step, feed_dict={xs: x_data, ys:y_data})
if i % 20 == 0:
print session.run(loss, feed_dict={xs: x_data, ys:y_data})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/OceanHorn/TensorFlowDemo.git
git@gitee.com:OceanHorn/TensorFlowDemo.git
OceanHorn
TensorFlowDemo
TensorFlowDemo
master

搜索帮助