Advertisement
Alaricy

Untitled

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