Skip to content

Commit

Permalink
consistency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Dec 6, 2019
1 parent 48c9f8f commit 13f8689
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 446 deletions.
192 changes: 114 additions & 78 deletions ch13/ch13_part1.ipynb

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions ch13/ch13_part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@

print(t.numpy())

t_splits = tf.split(t, [3, 2])
t_splits = tf.split(t, num_or_size_splits=[3, 2])

[item.numpy() for item in t_splits]

Expand Down Expand Up @@ -345,6 +345,17 @@



tf.random.set_seed(1)

## Order 1: shuffle -> batch -> repeat
ds = ds_joint.shuffle(4).batch(2).repeat(3)

for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())




tf.random.set_seed(1)

## Order 1: shuffle -> batch -> repeat
Expand All @@ -356,6 +367,17 @@



tf.random.set_seed(1)

## Order 2: batch -> shuffle -> repeat
ds = ds_joint.batch(2).shuffle(4).repeat(3)

for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())




tf.random.set_seed(1)

## Order 2: batch -> shuffle -> repeat
Expand Down Expand Up @@ -560,7 +582,6 @@ def load_and_preprocess(path, label):
ax.imshow(image[:, :, 0], cmap='gray_r')
ax.set_title('{}'.format(label), size=15)

#plt.savefig('ch13-mnist-new.png')
plt.show()


Expand Down
445 changes: 220 additions & 225 deletions ch13/ch13_part2.ipynb

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions ch13/ch13_part2.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,19 @@ def train(model, inputs, outputs, learning_rate):



model = tf.keras.Sequential([
iris_model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='sigmoid',
name='fc1', input_shape=(4,)),
tf.keras.layers.Dense(3, name='fc2', activation='softmax')])

model.summary()
iris_model.summary()




model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
iris_model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])



Expand All @@ -291,9 +291,9 @@ def train(model, inputs, outputs, learning_rate):
ds_train = ds_train.prefetch(buffer_size=1000)


history = model.fit(ds_train, epochs=num_epochs,
steps_per_epoch=steps_per_epoch,
verbose=0)
history = iris_model.fit(ds_train, epochs=num_epochs,
steps_per_epoch=steps_per_epoch,
verbose=0)



Expand Down Expand Up @@ -322,30 +322,30 @@ def train(model, inputs, outputs, learning_rate):



results = model.evaluate(ds_test.batch(50), verbose=0)
results = iris_model.evaluate(ds_test.batch(50), verbose=0)
print('Test loss: {:.4f} Test Acc.: {:.4f}'.format(*results))


# ### Saving and reloading the trained model



model.save('iris-classifier.h5',
overwrite=True,
include_optimizer=True,
save_format='h5')
iris_model.save('iris-classifier.h5',
overwrite=True,
include_optimizer=True,
save_format='h5')




model_new = tf.keras.models.load_model('iris-classifier.h5')
iris_model_new = tf.keras.models.load_model('iris-classifier.h5')

model_new.summary()
iris_model_new.summary()




results = model_new.evaluate(ds_test.batch(50), verbose=0)
results = iris_model_new.evaluate(ds_test.batch(50), verbose=0)
print('Test loss: {:.4f} Test Acc.: {:.4f}'.format(*results))


Expand All @@ -363,7 +363,7 @@ def train(model, inputs, outputs, learning_rate):



model.to_json()
iris_model_new.to_json()


# ## Choosing activation functions for multilayer neural networks
Expand Down
16 changes: 8 additions & 8 deletions ch14/ch14_part1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"output_type": "stream",
"text": [
"Sebastian Raschka & Vahid Mirjalili \n",
"last updated: 2019-12-04 \n",
"last updated: 2019-12-06 \n",
"\n",
"numpy 1.17.3\n",
"numpy 1.17.4\n",
"scipy 1.3.1\n",
"matplotlib 3.1.0\n",
"tensorflow 2.0.0\n"
Expand Down Expand Up @@ -783,8 +783,8 @@
],
"source": [
"model = tf.keras.Sequential()\n",
"model.add(tf.keras.layers.Dense(16, activation='relu'))\n",
"model.add(tf.keras.layers.Dense(32, activation='relu'))\n",
"model.add(tf.keras.layers.Dense(units=16, activation='relu'))\n",
"model.add(tf.keras.layers.Dense(units=32, activation='relu'))\n",
"\n",
"## late variable creation\n",
"model.build(input_shape=(None, 4))\n",
Expand Down Expand Up @@ -1487,9 +1487,9 @@
"\n",
"model = tf.keras.Sequential([\n",
" NoisyLinear(4, noise_stddev=0.1),\n",
" tf.keras.layers.Dense(4, activation='relu'),\n",
" tf.keras.layers.Dense(4, activation='relu'),\n",
" tf.keras.layers.Dense(1, activation='sigmoid')])\n",
" tf.keras.layers.Dense(units=4, activation='relu'),\n",
" tf.keras.layers.Dense(units=4, activation='relu'),\n",
" tf.keras.layers.Dense(units=1, activation='sigmoid')])\n",
"\n",
"model.build(input_shape=(None, 2))\n",
"model.summary()\n",
Expand Down Expand Up @@ -1557,7 +1557,7 @@
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook ch14_part1.ipynb to script\n",
"[NbConvertApp] Writing 19958 bytes to ch14_part1.py\n"
"[NbConvertApp] Writing 19996 bytes to ch14_part1.py\n"
]
}
],
Expand Down
10 changes: 5 additions & 5 deletions ch14/ch14_part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ def compute_z(x):


model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(units=16, activation='relu'))
model.add(tf.keras.layers.Dense(units=32, activation='relu'))

## late variable creation
model.build(input_shape=(None, 4))
Expand Down Expand Up @@ -729,9 +729,9 @@ def get_config(self):

model = tf.keras.Sequential([
NoisyLinear(4, noise_stddev=0.1),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(4, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')])
tf.keras.layers.Dense(units=4, activation='relu'),
tf.keras.layers.Dense(units=4, activation='relu'),
tf.keras.layers.Dense(units=1, activation='sigmoid')])

model.build(input_shape=(None, 2))
model.summary()
Expand Down
66 changes: 9 additions & 57 deletions ch14/ch14_part2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,7 @@
"INFO:tensorflow:global_step/sec: 567.788\n",
"INFO:tensorflow:loss = 240.40808, step = 4800 (0.176 sec)\n",
"INFO:tensorflow:global_step/sec: 587.168\n",
"INFO:tensorflow:loss = 319.34958, step = 4900 (0.170 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 319.34958, step = 4900 (0.170 sec)\n",
"INFO:tensorflow:global_step/sec: 613.08\n",
"INFO:tensorflow:loss = 270.3896, step = 5000 (0.163 sec)\n",
"INFO:tensorflow:global_step/sec: 615.585\n",
Expand Down Expand Up @@ -1174,13 +1168,7 @@
"INFO:tensorflow:global_step/sec: 653.213\n",
"INFO:tensorflow:loss = 69.10744, step = 13100 (0.153 sec)\n",
"INFO:tensorflow:global_step/sec: 661.769\n",
"INFO:tensorflow:loss = 66.7493, step = 13200 (0.151 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 66.7493, step = 13200 (0.151 sec)\n",
"INFO:tensorflow:global_step/sec: 653.19\n",
"INFO:tensorflow:loss = 50.434845, step = 13300 (0.153 sec)\n",
"INFO:tensorflow:global_step/sec: 662.245\n",
Expand Down Expand Up @@ -1345,13 +1333,7 @@
"INFO:tensorflow:loss = 26.513557, step = 21300 (0.178 sec)\n",
"INFO:tensorflow:global_step/sec: 640.564\n",
"INFO:tensorflow:loss = 21.903809, step = 21400 (0.156 sec)\n",
"INFO:tensorflow:global_step/sec: 647.2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:global_step/sec: 647.2\n",
"INFO:tensorflow:loss = 30.5953, step = 21500 (0.154 sec)\n",
"INFO:tensorflow:global_step/sec: 638.928\n",
"INFO:tensorflow:loss = 27.62001, step = 21600 (0.157 sec)\n",
Expand Down Expand Up @@ -1516,13 +1498,7 @@
"INFO:tensorflow:global_step/sec: 638.647\n",
"INFO:tensorflow:loss = 53.59935, step = 29600 (0.157 sec)\n",
"INFO:tensorflow:global_step/sec: 641.218\n",
"INFO:tensorflow:loss = 1.7887146, step = 29700 (0.156 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 1.7887146, step = 29700 (0.156 sec)\n",
"INFO:tensorflow:global_step/sec: 632.812\n",
"INFO:tensorflow:loss = 9.185707, step = 29800 (0.158 sec)\n",
"INFO:tensorflow:global_step/sec: 636.626\n",
Expand Down Expand Up @@ -1687,13 +1663,7 @@
"INFO:tensorflow:loss = 6.9836364, step = 37800 (0.157 sec)\n",
"INFO:tensorflow:global_step/sec: 640.438\n",
"INFO:tensorflow:loss = 3.7009566, step = 37900 (0.156 sec)\n",
"INFO:tensorflow:global_step/sec: 652.57\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:global_step/sec: 652.57\n",
"INFO:tensorflow:loss = 15.457409, step = 38000 (0.153 sec)\n",
"INFO:tensorflow:global_step/sec: 641.224\n",
"INFO:tensorflow:loss = 15.640904, step = 38100 (0.156 sec)\n",
Expand Down Expand Up @@ -2007,13 +1977,7 @@
"INFO:tensorflow:global_step/sec: 388.812\n",
"INFO:tensorflow:loss = 0.2290253, step = 4380 (0.273 sec)\n",
"INFO:tensorflow:global_step/sec: 373.834\n",
"INFO:tensorflow:loss = 0.24748756, step = 4480 (0.270 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 0.24748756, step = 4480 (0.270 sec)\n",
"INFO:tensorflow:global_step/sec: 373.023\n",
"INFO:tensorflow:loss = 0.2879139, step = 4580 (0.275 sec)\n",
"INFO:tensorflow:global_step/sec: 364.77\n",
Expand Down Expand Up @@ -2177,13 +2141,7 @@
"INFO:tensorflow:global_step/sec: 303.166\n",
"INFO:tensorflow:loss = 0.018593434, step = 12580 (0.310 sec)\n",
"INFO:tensorflow:global_step/sec: 307.677\n",
"INFO:tensorflow:loss = 0.009453268, step = 12680 (0.318 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 0.009453268, step = 12680 (0.318 sec)\n",
"INFO:tensorflow:global_step/sec: 323.497\n",
"INFO:tensorflow:loss = 0.0074377223, step = 12780 (0.317 sec)\n",
"INFO:tensorflow:global_step/sec: 318.278\n",
Expand Down Expand Up @@ -2343,13 +2301,7 @@
"INFO:tensorflow:global_step/sec: 280.449\n",
"INFO:tensorflow:loss = 0.0008953349, step = 20580 (0.364 sec)\n",
"INFO:tensorflow:global_step/sec: 278.265\n",
"INFO:tensorflow:loss = 0.0015090622, step = 20680 (0.371 sec)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:tensorflow:loss = 0.0015090622, step = 20680 (0.371 sec)\n",
"INFO:tensorflow:global_step/sec: 270.082\n",
"INFO:tensorflow:loss = 0.0010438098, step = 20780 (0.374 sec)\n",
"INFO:tensorflow:global_step/sec: 267.97\n",
Expand Down Expand Up @@ -2501,7 +2453,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.1"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 13f8689

Please sign in to comment.