链接镇楼:下面是莫烦大佬的github代码地址:
首先觉得大佬长得蛮帅的,然后声音特别悦耳动人!!!上一张图(如有侵犯,立马删)(划重点),接下来记录一些笔记。
第一章就不看了,大部分就是讲一下历史及安装好tensorfow ,下面主要记录从第二章开始。
Tensorflow 基础构架
通过一个线性回归的example来加强对tf使用的了解,附上代码及注释:
import numpy as np
import tensorflow as tf
#输入
x_data=np.random.rand(100).astype(np.float32)
y_data=x_data*0.1+0.3 #目标
##建立tensorflow 模型
Weight=tf.Variable(tf.random_uniform([1],-1,1)) #前面是维度,后面是随机数范围
Bias=tf.Variable(tf.zeros([1]))
y=Weight*x_data+Bias
loss=tf.reduce_mean(tf.square(y-y_data)) #定义loss
optimizer=tf.train.GradientDescentOptimizer(0.5)#定义optimizer 0.5为初始值
train=optimizer.minimize(loss) #训练过程,最小化loss
init=tf.initialize_all_variables() #有变量一定要初始化(不要忘记了)
sess=tf.Session()
sess.run(init) #激活tensorflow (重要)
for step in range(201):
sess.run(train)
if step % 20==0:
print(step,sess.run(Weight),sess.run(Bias))
主要讲了两种形式的sess
如果要使用sess=tf.Session()这种格式,每次sess.close()后若想继续执行一个sess必须重新tf.Session(),比较麻烦,而且sess.close()很容易忘记。
with tf.Session() as sess 使用其他语句会自动close,感觉比较好用
import tensorflow as tf
import numpy as np
m1=tf.constant([[5,3]])
m2=tf.constant([[2],[5]])
product=tf.matmul(m1,m2)
sess=tf.Session()
result=sess.run(product)
print(result)
with tf.Session() as sess:
result1=sess.run(product)
print(result1)
这个assign函数就是将new_value 付给state ,变量加常量还是变量
import tensorflow as tf
state = tf.Variable(0, name='counter')
#print(state.name)
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
init = tf.global_variables_initializer() #别忘了
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))
从外部传输要用Placeholder,而且通常与feed_dict配套用
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
print(sess.run(output,feed_dict={input1:[7.],input2:[8.]})) #placeholder 与feed_dict一起
激活函数了解一些,暂时就不看了