CoCalc Shared Filestensorflow.html
Author: Harald Schilly
Views : 19
Description: Jupyter html version of tensorflow.ipynb
(File too big to render with math typesetting.)
tensorflow

# Tensorflow on SageMathCloud¶

Make sure to use the Jupyter Kernel "Anaconda (Python 3)"

In [1]:
# check your version of python
import sys
sys.version

Out[1]:
'3.5.1 |Anaconda 2.4.1 (64-bit)| (default, Dec  7 2015, 11:16:01) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
In [2]:
# just some general setup
import numpy as np
from IPython.display import clear_output, Image, display
%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (14, 8)


## Say Hello to Tensorflow!¶

In [3]:
import tensorflow as tf
print(tf.__version__)
tf

0.6.0

Out[3]:
<module 'tensorflow' from '/projects/anaconda3/lib/python3.5/site-packages/tensorflow/__init__.py'>
In [4]:
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
sess.run(hello)

Out[4]:
b'Hello, TensorFlow!'
In [5]:
a = tf.constant(10)
b = tf.constant(32)
sess.run(a + b)

Out[5]:
42
In [ ]:


In [ ]:



### Visualization Function¶

In [6]:
def DisplayFractal(a, fmt='jpeg'):
"""
Display an array of iteration counts as a
colorful picture of a fractal.
"""
a_cyclic = (2 * np.pi * a / 20.0)[:,:,np.newaxis] # .reshape(list(a.shape)+[1])
img = np.concatenate([10 + 20 * np.cos(a_cyclic),
30 + 50 * np.sin(a_cyclic),
155 - 80 * np.cos(a_cyclic)], 2)
img[a==a.max()] = 0
a = img
a = np.uint8(np.clip(a, 0, 255))
plt.imshow(a)

In [ ]:



## Mandelbrot Set (from the Tutorial)¶

In [7]:
Y, X = np.mgrid[-1.3:1.3:0.005, -2.1:.9:0.005]
Z = X+1j*Y

In [8]:
sess = tf.InteractiveSession()

In [9]:
xs = tf.constant(Z.astype("complex64"))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, "float32"))

In [10]:
tf.initialize_all_variables().run()

In [11]:
# Compute the new values of z: z^2 + x
zs_ = zs*zs + xs

# Have we diverged with this new value?
not_diverged = tf.complex_abs(zs_) < 4

# Operation to update the zs and the iteration count.
#
# Note: We keep computing zs after they diverge! This
#       is very wasteful! There are better, if a little
#       less simple, ways to do this.
#
step = tf.group(
zs.assign(zs_),
)

In [12]:
%time
for i in range(200):
step.run()

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 4.29 Âµs

In [13]:
%time
DisplayFractal(ns.eval())

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 7.87 Âµs

In [14]:
sess.close()

In [ ]:


In [ ]:


In [ ]:



## Julia Set¶

In [15]:
Y, X = np.mgrid[-1.3:1.3:0.005, -1.3:1.3:0.005]
Z = X + 1j*Y
C = 0.12 + .63j

In [16]:
sess = tf.InteractiveSession()

In [17]:
xx = tf.constant(Z.astype("complex64"))

jwork = tf.Variable(xx)
jconst = tf.constant(np.complex64(C))
julia = tf.Variable(tf.zeros_like(xx, "float32"))

tf.initialize_all_variables().run()

In [18]:
jwork_ = jwork*jwork + jconst

not_diverged = tf.complex_abs(jwork_) < 4

julia_step = tf.group(
jwork.assign(jwork_),
)

In [19]:
%time
for i in range(200):
julia_step.run()

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 5.01 Âµs

In [20]:
%time
DisplayFractal(julia.eval())

CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 4.77 Âµs

In [21]:
sess.close()

In [ ]:



## Does skflow also work?¶

https://github.com/tensorflow/skflow/

In [22]:
import random

from sklearn import datasets, cross_validation, metrics
from sklearn import preprocessing

import skflow

random.seed(42)

X, y = boston.data, boston.target

# Split dataset into train / test
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y,
test_size=0.2, random_state=42)

# scale data (training set) to 0 mean and unit Std. dev
scaler = preprocessing.StandardScaler()
X_train = scaler.fit_transform(X_train)

# Build 2 layer fully connected DNN with 10, 10 units respecitvely.
regressor = skflow.TensorFlowDNNRegressor(hidden_units=[10, 10],
steps=5000, learning_rate=0.1, batch_size=1)

# Fit
regressor.fit(X_train, y_train)

# Predict and score
score = metrics.mean_squared_error(regressor.predict(scaler.fit_transform(X_test)), y_test)

print('MSE: {0:f}'.format(score))

Step #1, avg. loss: 436.31860
Step #501, epoch #1, avg. loss: 59.76304
Step #1001, epoch #2, avg. loss: 47.52392
Step #1501, epoch #3, avg. loss: 41.77745
Step #2001, epoch #4, avg. loss: 37.83664
Step #2501, epoch #6, avg. loss: 34.68758
Step #3001, epoch #7, avg. loss: 40.50972
Step #3501, epoch #8, avg. loss: 33.20059
Step #4001, epoch #9, avg. loss: 34.44275
Step #4501, epoch #11, avg. loss: 29.12216
MSE: 29.978488

In [ ]:


In [ ]:


In [ ]:


In [23]:
with tf.Session() as sess:
x = tf.Variable(21)
tf.initialize_all_variables().run()
tf.group(x.assign(x + x)).run()
print(x.eval())

42

In [ ]:


;