tensorflow学习(一)
暑假没有学习动力。。。所以通过边学习边写博客来激励自己咯
买了一本tensorflow深度学习实践,希望自己能看玩并在博客里总结。
第一次写博客,有错误希望大神指正 ^ ^。
大纲:
- tensorflow的常量、变量和数据类型
- 常量创建
- 变量创建
- 占位符创建
- tensorflow中的常用运算函数
- tensorflow入门程序
tensorflow的常量、变量和数据类型
在tensorflow中,数据是通过张量这个数据结构来表示的,即一个多维数组。
常量创建:
# 常量创建
tf.constant( value,
dtype=None,
shape=None,
name='Const',
verify_shape=False)
其中:
value是常量的值,可以是数字、列表或者字符串
dtype是该常量的数据类型有
shape是该常量的形状
name可以是任意字符串
verify_shape默认为False,当设置为true时,若value列表的数量与shape不匹配时会报错
例:
hello = tf.constant([1, 2, 3],
dtype=tf.int16,
shape=[2, 3],
verify_shape=True)
sess = tf.Session()
print(sess.run(hello))
结果报错:
TypeError: Expected Tensor's shape: (2, 3), got (3,).
变量创建
tf.Variable(value, dtype=None)
同样,变量也可以设置dtype等参数
例:
hello = tf.Variable([1.0, 2.0, 3.0], dtype=tf.float32)
print(hello)
结果:
<tf.Variable 'Variable:0' shape=(3,) dtype=float32_ref>
占位符创建
在tensorflow中,需要在外界数据被传入之前构建好整个数据流图,所以需要使用占位符来保留外界数据的位置,然后再图运行时再进行赋值
tf.placeholder(
dtype,
shape=None,
name=None
)
其中dtype等参数与常量的参数一致
例:
input1 = tf.placeholder(tf.int32)
input2 = tf.placeholder(tf.int32)
output = tf.add(input1, input2)
sess = tf.Session()
print(sess.run(output, feed_dict={input1: [1], input2: [2]}))
结果:
[3]
其中tf.add是tensorflow的加法函数,通过feed_dict参数将1和2分别给input1和input2赋值。
tensorflow中的常用运算函数
函数 | 描述 |
---|---|
tf.add(x, y, name=None) | 加法 |
tf.sub(x, y, name=None) | 减法 |
tf.mul(x, y , name=None) | 乘法 |
tf.div(x, y, name=None) | 除法 |
tf.mod(x, y, name=None) | 取模 |
tf.abs(x, y, name=None) | 取绝对值 |
tf.neg(x, y, name=None) | 取负 |
tensorflow入门程序
本节我们将实现一个类似于线性拟合的单层神经网络来作为tensoflow的入门程序。
首先,自定义一个数据集
inputx = np.random.rand(3000, 1)
noise = np.random.normal(0, 0.05, inputx.shape)
outputy = inputx*4+1+noise
由此可知,定义的数据集是一个线性函数:
y=4x+1
数据集的大小为3000个
然后,定义要训练的变量weight和bias
weight1 = tf.Variable(np.random.rand(inputx.shape[1], 1))
bias1 = tf.Variable(np.random.rand(inputx.shape[1], 1))
定义输入输出的占位符
x1 = tf.placeholder(tf.float64, [None, 1])
y = tf.placeholder(tf.float64, [None, 1])
定义输出
y1_ = tf.matmul(x1, weight1)+bias1
定义损失函数并选择梯度下降法(这里采用最小二乘法的损失函数,会在之后进行详细说明)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y1_ - y), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.25).minimize(loss)
初始化所有数值
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
至此,整个网络模型已经搭建完成,之后只需向占位符赋值即能启动该模型
启动模型,迭代1000次并输出weght和bias的值。
for i in range(1000):
sess.run(train, feed_dict={x1: inputx, y:outputy})
print("weight:", weight1.eval(sess))
print("----------")
print("bias:",bias1.eval(sess))
print("----------")
运行结果
weight: [[4.00095686]]
----------
bias: [[1.00055954]]
----------
完整代码:
import tensorflow as tf
import numpy as np
inputx = np.random.rand(3000, 1)
noise = np.random.normal(0, 0.05, inputx.shape)
outputy = inputx*4+1+noise
#1
weight1 = tf.Variable(np.random.rand(inputx.shape[1], 1))
bias1 = tf.Variable(np.random.rand(inputx.shape[1], 1))
x1 = tf.placeholder(tf.float64, [None, 1])
y = tf.placeholder(tf.float64, [None, 1])
y1_ = tf.matmul(x1, weight1)+bias1
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y1_ - y), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.25).minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train, feed_dict={x1: inputx, y:outputy})
print("weight:", weight1.eval(sess))
print("----------")
print("bias:",bias1.eval(sess))
print("----------")
欢迎评论求点赞
我爱学习学习爱我