mac下安裝tensorflow及入門例子


https://www.tensorflow.org/install/install_mac 

使用virtualenv安裝,virtualenv相當於使tensorflow運行在虛擬機環境下。

需要使用source ~/tensorflow/bin/activate 命令,(我安裝在了

source /Users/work/builded/tensorflow/bin/activate,bash下/是硬盤根目錄,~/是用戶根目錄)

命令行變成 (tensorflow)$ 這樣,才可以使用 import tensorflow as tf這些

 

使用ptython運行一個簡單的y = -x +1訓練模型:

(tensorflow) $ python tftrain.py

import numpy as np
import tensorflow as tf

# Model parameters
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss  = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM