莫烦 tensorflow 笔记 (一)搭建神经网络
来源:互联网 发布:windows升级助手 编辑:程序博客网 时间:2024/06/06 20:32
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Wed Aug 16 08:56:03 2017@author: xiaolian"""# add layersimport tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# inputs 输入值# in_size 输入的大小 # out_size ‘输出的大小# activation_function 激励函数def add_layer(inputs, in_size, out_size, activation_function = None): Weights = tf.Variable(tf.random_normal([in_size, out_size])) #tf.cast(Weights, tf.float64) #tf.cast(inputs, tf.float64) biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) #tf.cast(biases, tf.float64) z = tf.matmul(inputs, Weights) + biases if activation_function is None: outputs = z else: outputs = activation_function(z) return outputsx_data = np.linspace(-1, 1, 300)[:,np.newaxis].astype(np.float32)noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)y_data = (np.square(x_data) - 0.5 + noise).astype(np.float32)xs = tf.placeholder(tf.float32, [None, 1])ys = tf.placeholder(tf.float32, [None, 1])l1 = add_layer(x_data, 1, 10, activation_function = tf.nn.relu)# add output layerprediction = add_layer(l1, 10, 1, activation_function = None)# the error between prediction and real dataloss = tf.reduce_mean(tf.reduce_sum( \ tf.square(ys - prediction), reduction_indices = 1))train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)# init variableinit = tf.global_variables_initializer()sess = tf.Session()sess.run(init)# visualizationfig = plt.figure()ax = fig.add_subplot(1,1,1)ax.scatter(x_data, y_data)plt.ion()for i in range(1000): sess.run(train_step, feed_dict = {xs:x_data, ys:y_data}) if i == 999: print(sess.run(loss, feed_dict = {xs:x_data, ys:y_data})) prediction_value = sess.run(prediction, feed_dict = {xs:x_data}) line = ax.plot(x_data, prediction_value, 'g', lw = 5)plt.show()
输出:
阅读全文
2 0
- 莫烦 tensorflow 笔记 (一)搭建神经网络
- TensorFlow学习笔记(九)tf搭建神经网络基本流程
- TensorFlow学习笔记(十)tf搭建神经网络可视化结果
- TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
- Tensorflow 搭建神经网络(单层)
- 一文学会用 Tensorflow 搭建神经网络
- 一文学会用 Tensorflow 搭建神经网络
- 一文学会用 Tensorflow 搭建神经网络
- 一文学会用 Tensorflow 搭建神经网络
- 一文学会用 Tensorflow 搭建神经网络
- 一文学会用 Tensorflow 搭建神经网络
- TensorFlow学习笔记5:神经网络搭建
- 【Tensorflow】一文学会用 Tensorflow 搭建神经网络
- tensorflow之搭建神经网络
- tensorflow 学习笔记4 搭建神经网络基本流程
- TensorFlow学习笔记(二):实现神经网络
- TensorFlow学习笔记(三):深层神经网络
- TensorFlow笔记(一)
- IO流文件的读取与写入
- Qt5.7 编译V-REP
- Codeforces Round #429 (Div. 2) 题解(ABCD)
- hdu 1757 A Simple Math Problem
- delphi: reference to(匿名方法)
- 莫烦 tensorflow 笔记 (一)搭建神经网络
- 补一下我曾经拉下的东西——dfs
- ios的app内购研究
- bzoj 1083: [SCOI2005]繁忙的都市 并查集
- java常用代码
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- Drools Workbench 7.2.0.Final综合学习-驾照申请案例
- faster rcnn在Ubuntu系统下cpu模式的算法实现
- C++记录程序运行的时间