Advertisement
manul1537

Untitled

May 29th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. from tensorflow.keras.datasets import fashion_mnist
  2. from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D
  3. from tensorflow.keras.models import Sequential
  4. from tensorflow.keras.optimizers import Adam
  5. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  6. import numpy as np
  7.  
  8.  
  9.  
  10. def load_train(path):
  11.     features_train = np.load(path + 'train_features.npy')
  12.     target_train = np.load(path + 'train_target.npy')
  13.     features_train = features_train / 255
  14.     features_train = np.expand_dims(features_train, axis=3)
  15.  
  16.     return features_train, target_train
  17.  
  18. def load_test(path):
  19.     features_test = np.load(path + 'test_features.npy')
  20.     target_test = np.load(path + 'test_target.npy')
  21.     features_test = features_test / 255
  22.     features_test = np.expand_dims(features_test, axis=3)
  23.  
  24.     return features_test, target_test
  25.  
  26.  
  27. def create_model(input_shape):
  28.     model = Sequential()
  29.     model.add(Conv2D(filters=4, kernel_size=(3, 3), padding='same',
  30.                  activation="relu", input_shape=input_shape))
  31.     model.add(MaxPooling2D(pool_size=(2, 2)))
  32.     model.add(Flatten())
  33.     model.add(Dense(units=10, activation='softmax'))
  34.     optimizer = Adam(lr=0.005)
  35.     model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
  36.                   metrics=['acc'])
  37.  
  38.  
  39.     return model
  40.  
  41.  
  42.  
  43. def train_model(model, train_data, test_data, batch_size=None, epochs=47,
  44.                steps_per_epoch=None, validation_steps=None):
  45.  
  46.  
  47.     features_train, target_train = train_data
  48.     features_test, target_test = test_data
  49.     model.fit(features_train, target_train,
  50.               validation_data=(features_test, target_test),
  51.               batch_size=batch_size, epochs=epochs,
  52.               steps_per_epoch=steps_per_epoch,
  53.               validation_steps=validation_steps,
  54.               verbose=2, shuffle=True)
  55.  
  56.  
  57.     return model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement