Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for tensorflow 2 #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions keras_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# -*- coding: utf-8 -*-
from collections import defaultdict
import numpy as np
from keras.models import save_model
import tensorflow as tf
import keras
from keras import backend as K
import tensorflow.compat.v1 as tf
from tensorflow.compat.v1 import keras
from tensorflow.compat.v1.keras import backend as K
from tensorflow.compat.v1.keras.models import save_model
import tqdm_utils


Expand Down
2 changes: 1 addition & 1 deletion week2/preprocessed_mnist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import keras
from tensorflow import keras


def load_dataset(flatten=False):
Expand Down
17 changes: 4 additions & 13 deletions week2/v2/digits_classification.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# set tf 1.x for colab\n",
"%tensorflow_version 1.x"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -38,7 +28,7 @@
"from sklearn.metrics import accuracy_score\n",
"from matplotlib import pyplot as plt\n",
"%matplotlib inline\n",
"import tensorflow as tf\n",
"import tensorflow.compat.v1 as tf\n",
"print(\"We're using TF\", tf.__version__)\n",
"\n",
"import sys\n",
Expand Down Expand Up @@ -176,7 +166,7 @@
"metadata": {},
"outputs": [],
"source": [
"import keras\n",
"from tensorflow import keras\n",
"\n",
"y_train_oh = keras.utils.to_categorical(y_train, 10)\n",
"y_val_oh = keras.utils.to_categorical(y_val, 10)\n",
Expand All @@ -192,6 +182,7 @@
"outputs": [],
"source": [
"# run this again if you remake your graph\n",
"tf.disable_eager_execution()\n",
"s = reset_tf_session()"
]
},
Expand Down Expand Up @@ -398,4 +389,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}
6 changes: 1 addition & 5 deletions week2/v2/grading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
# -*- coding: utf-8 -*-


def get_tensor_shape(t):
return [d.value for d in t.shape]


def get_tensors_shapes_string(tensors):
res = []
for t in tensors:
res.extend([str(v) for v in get_tensor_shape(t)])
res.extend([str(v) for v in t.shape])
return " ".join(res)
96 changes: 17 additions & 79 deletions week2/v2/ill-conditioned-demo.ipynb

Large diffs are not rendered by default.

15 changes: 3 additions & 12 deletions week2/v2/intro_to_tensorflow.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# set tf 1.x for colab\n",
"%tensorflow_version 1.x"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -48,10 +38,11 @@
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import tensorflow.compat.v1 as tf\n",
"import sys\n",
"sys.path.append(\"../..\")\n",
"from keras_utils import reset_tf_session\n",
"tf.disable_eager_execution()\n",
"s = reset_tf_session()\n",
"print(\"We're using TF\", tf.__version__)"
]
Expand Down Expand Up @@ -581,4 +572,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}
23 changes: 6 additions & 17 deletions week2/v2/mnist_with_keras.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# set tf 1.x for colab\n",
"%tensorflow_version 1.x"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -46,13 +36,12 @@
"%matplotlib inline\n",
"import tensorflow as tf\n",
"print(\"We're using TF\", tf.__version__)\n",
"import keras\n",
"from tensorflow import keras\n",
"print(\"We are using Keras\", keras.__version__)\n",
"\n",
"import sys\n",
"sys.path.append(\"../..\")\n",
"import keras_utils\n",
"from keras_utils import reset_tf_session"
"import keras_utils"
]
},
{
Expand Down Expand Up @@ -127,11 +116,11 @@
"outputs": [],
"source": [
"# building a model with keras\n",
"from keras.layers import Dense, Activation\n",
"from keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Activation\n",
"from tensorflow.keras.models import Sequential\n",
"\n",
"# we still need to clear a graph though\n",
"s = reset_tf_session()\n",
"K.clear_session()\n",
"\n",
"model = Sequential() # it is a feed-forward network without loops like in RNN\n",
"model.add(Dense(256, input_shape=(784,))) # the first layer must specify the input shape (replacing placeholders)\n",
Expand Down Expand Up @@ -259,4 +248,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}
2 changes: 1 addition & 1 deletion week2/v2/preprocessed_mnist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import keras
from tensorflow import keras


def load_dataset(flatten=False):
Expand Down
Loading