updating keras

This commit is contained in:
mhjensen
2020-10-09 06:45:29 +02:00
parent 6dd6d78510
commit 4461d2e24b
10 changed files with 152 additions and 132 deletions
+22 -16
View File
@@ -1116,23 +1116,19 @@ conda activate tf-gpu
Keras is a high level "neural network":"https://en.wikipedia.org/wiki/Application_programming_interface"
that supports Tensorflow, CTNK and Theano as backends.
If you have Tensorflow installed Keras is available through the *tf.keras* module.
If you have Anaconda installed you may run the following command
!bc pycod
conda install keras
!ec
Alternatively, if you have Tensorflow or one of the other supported backends install you may use the pip package manager:
!bc pycod
pip install keras
!ec
or look up the "instructions here":"https://keras.io/".
You can look up the "instructions here":"https://keras.io/" for more information.
We will to a large extent use _keras_ in this course.
!split
===== Collect and pre-process data =====
Let us look again at the MINST data set.
!bc pycod
# import necessary packages
import numpy as np
@@ -1203,6 +1199,13 @@ X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=t
!bc pycod
epochs = 100
batch_size = 100
n_neurons_layer1 = 100
n_neurons_layer2 = 50
n_categories = 10
eta_vals = np.logspace(-5, 1, 7)
lmbd_vals = np.logspace(-5, 1, 7)
def create_neural_network_keras(n_neurons_layer1, n_neurons_layer2, n_categories, eta, lmbd):
model = Sequential()
model.add(Dense(n_neurons_layer1, activation='sigmoid', kernel_regularizer=regularizers.l2(lmbd)))
@@ -2005,7 +2008,16 @@ plt.show()
!split
===== Importing Keras and Tensorflow =====
!bc pycod
from keras.utils import to_categorical
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Sequential #This allows appending layers to existing models
from tensorflow.keras.layers import Dense #This allows defining the characteristics of a particular layer
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
from tensorflow.keras import Conv2D
from tensorflow.keras import MaxPooling2D
from tensorflow.keras import Flatten
from sklearn.model_selection import train_test_split
# representation of labels
@@ -2023,13 +2035,7 @@ X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=t
===== Running with Keras =====
!bc pycod
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.regularizers import l2
from keras.optimizers import SGD
def create_convolutional_neural_network_keras(input_shape, receptive_field,
n_filters, n_neurons_connected, n_categories,