阅读扩展
TensorFlow 卷积神经网络
TensorFlow 循环神经网络
TensorFlow 序列预测
TensorFlow 官网示例
TensorFlow2.0❤️
基础运行示例
变量要经过初始化
才能使用
import tensorflow as tf
# 创建tf格式的变量
a = tf.Variable([[1], [2]])
b = tf.Variable([[1, 2, 3]])
c = tf.matmul(a, b)
# 全局变量初始化
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(c))
[[1 2 3]
[2 4 6]]
线性回归
from sklearn.datasets import make_regression
import tensorflow as tf, matplotlib.pyplot as mp
# 创建数据
x, y, coef = make_regression(n_features=1, noise=9, coef=True)
X = x.reshape(-1)
# 初始化W系数【shape=(1,),在[-1,1]的均匀分布中随机取值】
W = tf.Variable(tf.random_uniform([1], -1, 1))
# 初始化b系数【shape=(1,),值为0】
B = tf.Variable(tf.zeros([1]))
# 线性回归方程
Y = W * X + B
# 损失函数
loss = tf.reduce_mean(tf.square(Y - y))
# 梯度下降优化器,设置学习率</