Make sure to use the Jupyter Kernel "Anaconda (Python 3)"
# check your version of python
import sys
sys.version
# 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)
import tensorflow as tf
print(tf.__version__)
tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
sess.run(hello)
a = tf.constant(10)
b = tf.constant(32)
sess.run(a + b)
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)
Y, X = np.mgrid[-1.3:1.3:0.005, -2.1:.9:0.005]
Z = X+1j*Y
sess = tf.InteractiveSession()
xs = tf.constant(Z.astype("complex64"))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, "float32"))
tf.initialize_all_variables().run()
# 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_),
ns.assign_add(tf.cast(not_diverged, "float32"))
)
%time
for i in range(200):
step.run()
%time
DisplayFractal(ns.eval())
sess.close()
Y, X = np.mgrid[-1.3:1.3:0.005, -1.3:1.3:0.005]
Z = X + 1j*Y
C = 0.12 + .63j
sess = tf.InteractiveSession()
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()
jwork_ = jwork*jwork + jconst
not_diverged = tf.complex_abs(jwork_) < 4
julia_step = tf.group(
jwork.assign(jwork_),
julia.assign_add(tf.cast(not_diverged, "float32"))
)
%time
for i in range(200):
julia_step.run()
%time
DisplayFractal(julia.eval())
sess.close()
import random
from sklearn import datasets, cross_validation, metrics
from sklearn import preprocessing
import skflow
random.seed(42)
# Load dataset
boston = datasets.load_boston()
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))
with tf.Session() as sess:
x = tf.Variable(21)
tf.initialize_all_variables().run()
tf.group(x.assign(x + x)).run()
print(x.eval())