deniswhite77

ADAM

Feb 23rd, 2022 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. from tensorflow.keras import Sequential
  2. from tensorflow.keras.layers import Conv2D, Flatten, Dense, AvgPool2D
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from tensorflow.keras.optimizers import Adam
  6. optimizer = Adam()
  7.  
  8. def load_train(path):
  9.     features_train = np.load(path + 'train_features.npy')
  10.     target_train = np.load(path + 'train_target.npy')
  11.     features_train = features_train.reshape(features_train.shape[0], 28 * 28) / 255.
  12.     return features_train, target_train
  13.  
  14.  
  15. def create_model(input_shape):
  16.     model = Sequential()
  17.     model.add(Conv2D(filters=6, kernel_size=(5, 5), padding='same',
  18.                  activation='relu', input_shape=input_shape))
  19.     model.add(AvgPool2D(pool_size =(2,2)))
  20.     model.add(Conv2D(filters=16, kernel_size=(5, 5), padding='valid',
  21.                  activation='relu'))
  22.     model.add(AvgPool2D(pool_size =(2,2)))
  23.     model.add(Flatten())
  24.     model.add(Dense(units=120, activation='relu'))
  25.     model.add(Dense(units=10, activation='softmax'))
  26.     model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
  27.                   metrics=['acc'])
  28.  
  29.     return model
  30.  
  31.  
  32. def train_model(model, train_data, test_data, batch_size=32, epochs=5,
  33.                steps_per_epoch=None, validation_steps=None):
  34.  
  35.     features_train, target_train = train_data
  36.     features_test, target_test = test_data
  37.     model.fit(features_train, target_train,
  38.               validation_data=(features_test, target_test),
  39.               batch_size=batch_size, epochs=epochs,
  40.               steps_per_epoch=steps_per_epoch,
  41.               validation_steps=validation_steps,
  42.               verbose=2, shuffle=True)
  43.  
  44.     return model
Add Comment
Please, Sign In to add comment