Skip to content

Commit

Permalink
updated notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmpercussion committed Sep 9, 2024
1 parent 0de5737 commit 1ca75a9
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 121 deletions.
74 changes: 60 additions & 14 deletions notebooks/0-test-setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@
"metadata": {},
"outputs": [],
"source": [
"# install packages in the current Jupyter kernel\n",
"import sys\n",
"!{sys.executable} -m pip install -r ../requirements.txt\n",
"!{sys.executable} -m pip install tensorflow-macos\n",
"!{sys.executable} -m pip install --upgrade requests"
"# # install packages in the current Jupyter kernel\n",
"# import sys\n",
"# !{sys.executable} -m pip install -r ../requirements.txt\n",
"# !{sys.executable} -m pip install tensorflow-macos\n",
"# !{sys.executable} -m pip install --upgrade requests\n",
"\n",
"# Probably don't randomly install things this way, use Poetry instead."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -109,7 +104,8 @@
"from tensorflow import keras\n",
"\n",
"model = keras.models.Sequential()\n",
"model.add(keras.layers.Dense(units=64, activation='relu', input_dim=100))\n",
"model.add(keras.Input(shape=(100,)))\n",
"model.add(keras.layers.Dense(units=64, activation='relu'))\n",
"model.add(keras.layers.Dense(units=10, activation='softmax'))\n",
"model.compile(loss='categorical_crossentropy', optimizer='sgd')\n",
"model.summary()"
Expand Down Expand Up @@ -184,6 +180,56 @@
"img = keras.utils.array_to_img(doom_arrays[0], scale=False)\n",
"display(img.resize((300, 300)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Looking for physical devices.\n",
"\n",
"What hardware is available for neural network training and inference?"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import tensorflow as tf\n",
"tf.config.list_physical_devices()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/job:localhost/replica:0/task:0/device:CPU:0\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"tensor = tf.constant([])\n",
"print(tensor.device)"
]
}
],
"metadata": {
Expand All @@ -202,7 +248,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.5"
}
},
"nbformat": 4,
Expand Down
43 changes: 23 additions & 20 deletions notebooks/1-star-trek-titles-RNN-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"source": [
"## Much borrowed from https://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py\n",
"\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers\n",
"import keras\n",
"from keras import layers\n",
"import numpy as np\n",
"import random\n",
"import sys\n",
Expand Down Expand Up @@ -131,8 +131,8 @@
"source": [
"#X shape: 3D tensor. First dimension is the sentences, second is each letter in each sentence, third is the onehot\n",
"#vector representing that letter.\n",
"X = np.zeros((len(sentences), maxlen, vocabulary_size), dtype=np.bool)\n",
"y = np.zeros((len(sentences), vocabulary_size), dtype=np.bool)\n",
"X = np.zeros((len(sentences), maxlen, vocabulary_size), dtype=bool)\n",
"y = np.zeros((len(sentences), vocabulary_size), dtype=bool)\n",
" \n",
"for i, sentence in enumerate(sentences):\n",
" for t, char in enumerate(sentence):\n",
Expand Down Expand Up @@ -186,8 +186,8 @@
"layer_size = 128\n",
"# build the model: a single LSTM layer.\n",
"model_train = keras.Sequential()\n",
"\n",
"model_train.add(layers.LSTM(layer_size, input_shape=(maxlen, len(chars))))\n",
"model_train.add(keras.Input(shape=(maxlen, len(chars))))\n",
"model_train.add(layers.LSTM(layer_size))\n",
"# Project back to vocabulary. One output node for each letter.\n",
"# Dense indicates a fully connected layer.\n",
"# Softmax activation ensures the combined values of all outputs form a probability distribution:\n",
Expand Down Expand Up @@ -277,7 +277,7 @@
" x_pred[0, t, char_indices[char]] = 1.\n",
" \n",
"\n",
" predictions_distribution = generating_model.predict(x_pred, verbose=0)[0]\n",
" predictions_distribution = generating_model(x_pred)[0]\n",
" next_index = sample(predictions_distribution, diversity)\n",
" next_char = indices_char[next_index]\n",
"\n",
Expand All @@ -288,10 +288,13 @@
" return generated\n",
"\n",
"def generate_sample_text(epoch, logs):\n",
" # Function invoked at end of each epoch. Prints generated text.\n",
" generated = generate_text_segment(200, 1.0, model_train, input_sequence_length = maxlen)\n",
" print(\"Seed:\\n\", generated[:30], \"\\n\")\n",
" print(\"Generated text:\\n\", generated[30:], \"\\n\\n\")\n",
" # Function invoked at end of each fifth epoch. Prints generated text.\n",
" if epoch % 5 == 0:\n",
" generated = generate_text_segment(200, 1.0, model_train, input_sequence_length = maxlen)\n",
" print(\"\\nSeed:\\n\", generated[:30], \"\\n\")\n",
" print(\"\\nGenerated text:\\n\", generated[30:], \"\\n\\n\")\n",
" else:\n",
" return\n",
"\n",
"print_callback = keras.callbacks.LambdaCallback(on_epoch_end=generate_sample_text)"
]
Expand All @@ -316,7 +319,7 @@
},
"outputs": [],
"source": [
"history = model_train.fit(X, y, batch_size=128, epochs=50, callbacks=[print_callback])"
"history = model_train.fit(X, y, batch_size=128, epochs=51, callbacks=[print_callback])"
]
},
{
Expand All @@ -326,7 +329,7 @@
"outputs": [],
"source": [
"# Save model if necessary\n",
"model_train.save(\"keras-startrek-LSTM-model.h5\")"
"model_train.save(\"keras-startrek-LSTM-model.keras\")"
]
},
{
Expand Down Expand Up @@ -393,16 +396,16 @@
"# Build a decoding model (input length 1, batch size 1, stateful)\n",
"layer_size = 128\n",
"\n",
"model_dec = Sequential()\n",
"model_dec = keras.Sequential()\n",
"# 1 letter in, 1 letter out.\n",
"# Stateful=True keeps the state from the end of one batch to the start of the next\n",
"# In other words, the network \"remembers\" its state from one input to the next. This is essential when\n",
"# the network looks at 1 input at a time.\n",
"model_dec.add(LSTM(layer_size, stateful=True, batch_input_shape=(1,1,len(chars))))\n",
"model_dec.add(keras.Input(shape=(1, len(chars)), batch_size=1))\n",
"model_dec.add(layers.LSTM(layer_size, stateful=True))\n",
"\n",
"# project back to vocabulary\n",
"model_dec.add(Dense(vocabulary_size, activation='softmax'))\n",
"model_dec.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.01))\n",
"model_dec.add(layers.Dense(vocabulary_size, activation='softmax'))\n",
"model_dec.summary()\n",
"\n",
"# set weights from training model\n",
Expand All @@ -428,15 +431,15 @@
"outputs": [],
"source": [
"# Sample 1000 characters from the decoding model using a random seed from the vocabulary.\n",
"generated = generate_text_segment(1000, diversity=1.0, generating_model = model_dec, input_sequence_length = 1)\n",
"generated = generate_text_segment(500, diversity=1.0, generating_model = model_dec, input_sequence_length = 1)\n",
"sys.stdout.write(generated)\n",
"print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -450,7 +453,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.12.5"
},
"widgets": {
"state": {
Expand Down
Loading

0 comments on commit 1ca75a9

Please sign in to comment.