Commit c2a480bd authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+5 −0
Changes for Introduction to Deep Learning Models With TensorFlow - Working Files/Working Files - README.txt: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
Due to ongoing efforts to ensure you have the best possible learning experience, we may update the working files for this course from time to time.

We recommend that you download the latest version of these files from:

http://www.infiniteskills.com/02492
 No newline at end of file
+459 −0
Changes for Introduction to Deep Learning Models With TensorFlow - Working Files/data_types_and_tf_graph.ipynb: 459 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import tensorflow as tf
import numpy as np
```

%% Cell type:code id: tags:

``` python
x1 = np.random.randn(100,100)
x2 = np.random.randn(100,100)
x3 = np.random.randn(100,100)
```

%% Cell type:code id: tags:

``` python
y = np.matmul(x1,x2)
```

%% Cell type:code id: tags:

``` python
y[0,0]
```

%% Cell type:code id: tags:

``` python
y = np.matmul(x3,np.matmul(x2,x1))
```

%% Cell type:code id: tags:

``` python
y[0,0]
```

%% Cell type:code id: tags:

``` python
xt1 = tf.random_normal(shape=(100,100))
xt2 = tf.random_normal(shape=(100,100))
xt3 = tf.random_normal(shape=(100,100))
```

%% Cell type:code id: tags:

``` python
y = tf.matmul(xt3,tf.matmul(xt2,xt1))
```

%% Cell type:code id: tags:

``` python
y
```

%% Cell type:code id: tags:

``` python
y[0,0]
```

%% Cell type:code id: tags:

``` python
type(y)
```

%% Cell type:code id: tags:

``` python
sess = tf.Session()
```

%% Cell type:code id: tags:

``` python
out = sess.run(y)
```

%% Cell type:code id: tags:

``` python
out[0,0]
```

%% Cell type:code id: tags:

``` python
type(out)
```

%% Cell type:code id: tags:

``` python
out1, out2 = sess.run([y,xt1])
```

%% Cell type:code id: tags:

``` python
out1[0,0], out2[0,0]
```

%% Cell type:code id: tags:

``` python
xt4 = tf.random_normal(shape=(50,50))
```

%% Cell type:code id: tags:

``` python
sess.run(y)[0,0]
```

%% Cell type:code id: tags:

``` python
sess.close()
```

%% Cell type:code id: tags:

``` python
with tf.Session() as sess:
    out = sess.run(y)
```

%% Cell type:code id: tags:

``` python
out[0,0]
```

%% Cell type:code id: tags:

``` python
sess = tf.InteractiveSession()
```

%% Cell type:code id: tags:

``` python
out = y.eval()
```

%% Cell type:code id: tags:

``` python
out[0,0]
```

%% Cell type:code id: tags:

``` python
a = tf.Variable([0.0,1.0,2.0])
```

%% Cell type:code id: tags:

``` python
a
```

%% Cell type:code id: tags:

``` python
print(a.get_shape())
```

%% Cell type:code id: tags:

``` python
#SHOULD THROW EXCEPTION
a.eval()
```

%% Cell type:code id: tags:

``` python
a.initializer
```

%% Cell type:code id: tags:

``` python
sess.run(a.initializer)
```

%% Cell type:code id: tags:

``` python
a.eval()
```

%% Cell type:code id: tags:

``` python
b = tf.Variable(np.random.randn(10,10))
```

%% Cell type:code id: tags:

``` python
b
```

%% Cell type:code id: tags:

``` python
sess.run(b.initializer)
```

%% Cell type:code id: tags:

``` python
b.eval()[0,0]
```

%% Cell type:code id: tags:

``` python
d = tf.get_variable(name="d", shape=(2,2), dtype=tf.float32)
e = tf.get_variable(name="e", shape=(2,1), dtype=tf.float32)
f = tf.get_variable(name="f", shape=(2,2), dtype=tf.float32)
g = tf.get_variable(name="g", shape=(2,1), dtype=tf.float32)
```

%% Cell type:code id: tags:

``` python
h = tf.matmul(d,e) + tf.matmul(f,g)
```

%% Cell type:code id: tags:

``` python
h
```

%% Cell type:code id: tags:

``` python
init = tf.global_variables_initializer()
```

%% Cell type:code id: tags:

``` python
sess.run(init)
```

%% Cell type:code id: tags:

``` python
sess.run(h)
```

%% Cell type:code id: tags:

``` python
i = tf.get_variable("i", shape=(2,2), initializer=tf.constant_initializer(5.0))
```

%% Cell type:code id: tags:

``` python
sess.run(i.initializer)
```

%% Cell type:code id: tags:

``` python
i.eval()
```

%% Cell type:code id: tags:

``` python
j = tf.get_variable("j", shape=(2,2), initializer=tf.random_uniform_initializer(maxval=1.0, minval=0.0))
#tf.random_normal_initializer(mean=0.0, stddev=1.0)
#tf.random_uniform_initializer(maxval=0.05, minval=-0.05)
#Google NMT paper which uses random uniform [-0.04, 0.04] https://arxiv.org/pdf/1609.08144v2.pdf
```

%% Cell type:code id: tags:

``` python
sess.run(j.initializer)
```

%% Cell type:code id: tags:

``` python
j.eval()
```

%% Cell type:code id: tags:

``` python
p1 = tf.placeholder(name="holder1", shape=(2,), dtype=tf.float32)
p2 = tf.constant([5.0,3.0])
p3 = p1 + p2
```

%% Cell type:code id: tags:

``` python
#SHOULD THROW EXCEPTION
sess.run(p3)
```

%% Cell type:code id: tags:

``` python
sess.run(p3, feed_dict={p1:np.array([1.0,2.0])})
```

%% Cell type:code id: tags:

``` python
sess.run(p3, feed_dict={p1:np.array([5.0,10.0])})
```

%% Cell type:code id: tags:

``` python
placeholders = [tf.placeholder(shape=(1,), dtype=tf.float32) for i in range(5)]
val = tf.constant(10.0)
for i in range(5):
    val = val + placeholders[i]
```

%% Cell type:code id: tags:

``` python
feed_dict = {placeholders[i]:np.array([i]) for i in range(5)}
```

%% Cell type:code id: tags:

``` python
sess.run(val, feed_dict=feed_dict)
```

%% Cell type:code id: tags:

``` python
g = tf.get_default_graph()
```

%% Cell type:code id: tags:

``` python
g
```

%% Cell type:code id: tags:

``` python
sess.graph is g
```

%% Cell type:code id: tags:

``` python
ops = g.get_operations()
```

%% Cell type:code id: tags:

``` python
ops[:5]
```

%% Cell type:code id: tags:

``` python
[print(o.name) for o in ops[:5]]
```

%% Cell type:code id: tags:

``` python
trainable_vars = tf.trainable_variables()
```

%% Cell type:code id: tags:

``` python
[print(t.name, t.get_shape()) for t in trainable_vars]
```

%% Cell type:code id: tags:

``` python
with tf.variable_scope("model1") as scope:
    a = tf.get_variable("model", shape=(1), dtype=tf.int32, initializer=tf.constant_initializer(2))
with tf.variable_scope("model2") as scope:
    b = tf.get_variable("model", shape=(1), dtype=tf.int32, initializer=tf.constant_initializer(3))
c = a + b
```

%% Cell type:code id: tags:

``` python
sess.run([a.initializer, b.initializer])
sess.run(c)
```

%% Cell type:code id: tags:

``` python
[t.name for t in tf.trainable_variables()]
```

%% Cell type:code id: tags:

``` python
with tf.variable_scope("model3", reuse=False) as scope:
    a = tf.get_variable("model", shape=(1), dtype=tf.int32, initializer=tf.constant_initializer(2))
with tf.variable_scope("model3", reuse=True) as scope:
    b = tf.get_variable("model", shape=(1), dtype=tf.int32)
```

%% Cell type:code id: tags:

``` python
b is a
```

%% Cell type:code id: tags:

``` python
[t.name for t in tf.trainable_variables()]
```

%% Cell type:code id: tags:

``` python
with tf.variable_scope("prefix1"):
    with tf.variable_scope("prefix2"):
        tf.get_variable("model", shape=(1), dtype=tf.int32, initializer=tf.constant_initializer(2))
```

%% Cell type:code id: tags:

``` python
[t.name for t in tf.trainable_variables()]
```

%% Cell type:code id: tags:

``` python
```
+520 −0

File added.

Preview size limit exceeded, changes collapsed.

+1.64 KiB

File added.

No diff preview for this file type.

+880 B

File added.

No diff preview for this file type.

Loading