Python环境:mac OS Sierra 10.12.5 Anaconda4.4.0 Python3.6 打开Terminal,进入到安装目录的bin下面,
➜ bin ./conda list
列出安装的包。
现在需要安装tensorflow,输入
➜ bin ./anaconda search -t conda tensorflow
看到支持mac的都可以装,只要版本不太旧。
安装前可以进一步查看包的情况
➜ bin ./anaconda show conda-forge/tensorflow
最后一行指出了安装的命令
执行 ➜ bin ./conda install --channel https://conda.anaconda.org/conda-forge tensorflow
上述过程回答y
In [14]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.100, 0.200], x_data) + 0.300
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
x_axis = []
y_axis = []
for step in range(0, 201):
sess.run(train)
if step % 20 == 0:
x_axis.append(step)
b_result = sess.run(b)
y_axis.append(b_result[0])
print('%s,%s,%s' % (step, sess.run(W), b_result))
plt.plot(x_axis, y_axis)
plt.show()
In [ ]: