Facebook
From Morose Anoa, 1 Month ago, written in Plain Text.
This paste is a reply to Untitled from Speedy Pelican - view diff
Embed
Download Paste or View Raw
Hits: 62
  1. import numpy as np
  2. import tensorflow as tf
  3. import tensorflow.keras as keras
  4. from tensorflow.keras import Sequential
  5. from tensorflow.keras.layers import Conv2D, AvgPool2D, Flatten, Dense, Dropout
  6.  
  7.  
  8. def load_train(path):
  9.     x_train = np.load(path + 'train_features.npy')
  10.     y_train = np.load(path + 'train_target.npy')
  11.     x_train = x_train.reshape(x_train.shape[0],28,28,1)/255.0
  12.     return x_train, y_train
  13.    
  14. def create_model(input_shape):
  15.     model = Sequential()
  16.     model.add(Conv2D(filters = 32, kernel_size=(3,3),padding = 'same', activation = 'relu',input_shape=(28,28,1)))
  17.     model.add(AvgPool2D(pool_size=(2, 2)))
  18.     model.add(Conv2D(filters = 64, kernel_size=(3,3), activation = 'relu'))
  19.     model.add(AvgPool2D(pool_size=(2, 2)))
  20.     model.add(Conv2D(filters = 128, kernel_size=(3,3), activation = 'relu'))
  21.     model.add(Flatten())
  22.     model.add(Dense(units=100,activation = 'relu'))
  23.     model.add(Dense(units=50,activation = 'relu'))
  24.     model.add(Dense(units=30,activation = 'relu'))
  25.     model.add(Dense(units=10,activation = 'softmax'))
  26.     model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc'])
  27.     return model
  28.    
  29. def train_model(model, train_data, test_data, batch_size=32, epochs=5,
  30.                 steps_per_epoch=None, validation_steps=None):
  31.     x_train, y_train = train_data
  32.     x_test, y_test = test_data
  33.     model.fit(train_data, validation_data=test_data,batch_size=batch_size,
  34.               epochs=epochs, steps_per_epoch=steps_per_epoch,validation_steps=validation_steps,
  35.               verbose=2, shuffle=True)
  36.     return model