Skip to content

Instantly share code, notes, and snippets.

model = models.Sequential([
layers.Dense(128, activation = 'relu', input_shape = Xtrain[0].shape),
layers.Dense(64, activation = 'relu'),
#layers.Dense(16, activation = 'relu'),
layers.Dense(8, activation = 'relu'),
layers.Dense(1)
])
cb = callbacks.EarlyStopping(patience = 10, restore_best_weights = True)
# Splitting into train, val and test set -- 80-10-10 split
# First, an 80-20 split
train_df, val_test_df = train_test_split(df, test_size = 0.2)
# Then split the 20% into half
val_df, test_df = train_test_split(val_test_df, test_size = 0.5)
# Splitting into X (input) and y (output)
# defining the input and output columns to separate the dataset in the later cells.
input_columns = df.columns.tolist()
input_columns.remove('label')
output_columns = ['label']
df[['red','green','blue']] /= 255
# pick random test data sample from one batch
x = random.randint(0, len(Xtest) - 1)
output = model.predict(Xtest[x].reshape(1, -1))[0]
pred = np.argmax(output)
print("Predicted: ", pred, "(", output[pred], ")")
print("True: ", np.argmax(np.array(ytest)[x]))
model = models.Sequential([
layers.Dense(256, activation = 'relu', input_shape = Xtrain[0].shape),
layers.Dense(64, activation = 'relu'),
layers.Dense(16, activation = 'relu'),
layers.Dense(4, activation = 'softmax')
])
cb = [callbacks.EarlyStopping(patience = 5, restore_best_weights = True)]
model.compile(optimizer=optimizers.Adam(0.0001), loss=losses.CategoricalCrossentropy(), metrics=['accuracy'])
ss_scaler = StandardScaler()
# Fit on training set alone
Xtrain = ss_scaler.fit_transform(Xtrain)
# Use it to transform val and test input
Xval = ss_scaler.transform(Xval)
Xtest = ss_scaler.transform(Xtest)
# Splitting into train, val and test set -- 80-10-10 split
# First, an 80-20 split
Xtrain, Xvaltest, ytrain, yvaltest = train_test_split(X, y, test_size = 0.2)
# Then split the 20% into half
Xval, Xtest, yval, ytest = train_test_split(Xvaltest, yvaltest, test_size = 0.5)