Tensorflow学习笔记(一)tensorflow安装

笔记是根据莫烦老师的Tensorflow视频总结的。

 

1)windows安装cpu版tensorflow。

pip install tensorflow-cpu

 

等待安装完成之后开始写代码就行了

import tensorflow as tf
import os 
import numpy as np

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 忽略警告

x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3 # 

### create tensorflow structure start ###
weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) # 一维结构从-1.0 到 1.0
biases = tf.Variable(tf.zeros([1])) #

y= weights*x_data + biases

loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5) # 建立优化器, 0.5学习效率,通常设置为<1的数
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

### create tensorflow structure start ###

sess = tf.Session()
sess.run(init)

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(weights), sess.run(biases))

 

然后运行就行了....

Tensorflow学习笔记(一)tensorflow安装_第1张图片

你可能感兴趣的:(Tensorflow)