2017年5月7日 星期日

Google Cloud Platform GCP - Tensorflow Hello

 

本文介紹如何在 GCP install Anaconda and Tensorflow.  就我而言是最簡單的方式。

Step0: Create a VM (choose OS: Debian 8 或 Ubuntu TLS 14.04).  請參考前文。

Step1:  Install Anaconda

Reference: 

https://haroldsoh.com/2016/04/28/set-up-anaconda-ipython-tensorflow-julia-on-a-google-compute-engine-vm/

> mkdir downloads

> cd downloads

wget http://repo.continuum.io/archive/Anaconda3-4.3.1-Linux-x86_64.sh

bash Anaconda3-4.3.1-Linux-x86_64.sh

source ~/.bashrc

 

2017/8/13 update

Anaconda update to 4.4.0 version

 

 

Step2: Install Tensorflow

Reference:

https://www.tensorflow.org/install/install_linux#InstallingAnaconda

Anaconda install Tensorflow 非常容易也非常快 (because of GCP?) !

>  conda create -n tensorflow

> source activate tensorflow

再來是 install python tensorflow packages.  先 check python version: 3.6.0

(Tensorflow)$ > pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.1.0-cp36-cp36m-linux_x86_64.whl

 

2017/8/13 update

Tensorflow update to 1.2.1 version

 

下一步是確認 tensorflow 是否 ok.

Run python

>>>import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>>print(sess.run(hello))

 

Step3: Do a linear regression

參考以下 tutorial.

https://www.tensorflow.org/get_started/get_started

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))


沒有留言:

張貼留言

追蹤者