Tensorflow 搭建简单神经网络 | Python
来源:互联网 发布:大闹天宫数据库密码 编辑:程序博客网 时间:2024/05/21 10:16
本案例为回归问题,采用了一层隐层,隐层和输出层的激活函数都为 relu,损失函数MSE。
import tensorflow as tffrom numpy.random import RandomStatew1 = tf.Variable(tf.truncated_normal([2,3],seed=1))w2 = tf.Variable(tf.truncated_normal([3,1],seed=1))x = tf.placeholder(dtype=tf.float32,shape=[None,2]) #placeholder 存放 x:features ,y_real:labelsy_real = tf.placeholder(dtype=tf.float32,shape=[None,1])a = tf.nn.relu(tf.matmul(x,w1)) #神经元的激活函数为 relu;隐层y_pre = tf.nn.relu(tf.matmul(a,w2)) #输出层sample_size = 20000 #训练样本数量rds = RandomState(0) #随机种子X = rds.rand(sample_size,2)Y = [[int(20*x1+30*x2)]+rds.rand(1) for (x1,x2) in X] # y_label=20*x1+30*x2+noise MSE = tf.reduce_mean(abs(y_real-y_pre)) # 使用均方误差(MSE)作为损失函数train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(MSE) # 反向传播算法为:GradientDescentOptimizer,learing rate = 1e-3step = 20000 #训练迭代次数batch = 500 #批大小为 500start = 0 # 每个batch 的开始和结束指针end = batchsess = tf.Session()sess.run(tf.global_variables_initializer())for i in range(step): sess.run(train_step,feed_dict={x:X[start:end],y_real:Y[start:end]}) if not i%20: H = sess.run(MSE,feed_dict={x:X[start:end],y_real:Y[start:end]}) print ("MSE:",H) if H<0.4: #采用stop early 的方法防止过拟合,节省训练时间。 break strat = end if end<sample_size else 0 end = start+batchy1 = sess.run(y_pre,feed_dict={x:X[start:end]})y2 = Y[start:end]sess.close() #训练结果的部分展示for i in range(100): print(y1[i],y2[i])
阅读全文
0 0
- Tensorflow 搭建简单神经网络 | Python
- Python搭建tensorflow三层神经网络
- tensorflow之安装及简单神经网络搭建
- tensorflow之搭建神经网络
- 基于tensorflow的简单BP神经网络的结构搭建
- 利用tensorflow keras搭建一个简单的卷积神经网络
- 基于tensorflow的简单BP神经网络的结构搭建
- tensorflow之简单卷积神经网络(CNN)搭建
- 基于tensorflow的简单BP神经网络的结构搭建
- tensorflow构建简单神经网络
- Tensorflow简单的神经网络
- 用TensorFlow自己动手搭建神经网络
- tensorflow-搭建两层神经网络
- TensorFlow搭建CNN卷积神经网络
- Tensorflow 搭建神经网络(单层)
- 小白学Tensorflow之简单神经网络
- tensorflow:1)简单的神经网络
- tensorflow 仿真已知简单神经网络
- redis常用命令
- Mysql提示Column count doesn't match value count at row 1
- 小米手机,测试时应用图标不能更改,还出现偶尔应用版本还原
- 顺序播放效果js实现小窍门
- unity接入微信支付-Ios篇
- Tensorflow 搭建简单神经网络 | Python
- The Solution to Leetcode 101 Symmetric Tree
- JDBC连接数据库的代码及步骤
- 不同操作系统之间的网络配置
- Java实现-不同的路径1
- iframe页面中父页面跟子页面方法的相互调用
- weblogic发布程序中遇到的问题
- git 代码统计
- tensorflow54 《TensorFlow技术解析与实战》15 TensorFlow线性代数编译框架XLA