class AudioFeaturesModel: def __init__(self, model_name, le, layers): self.le = le self.model = Sequential(name=model_name) # Builds layers based on the structure in model_structures for layer in layers: self.model.add(layer) def compile(self): """Compile the model and print the structure""" self.model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam') self.model.summary() def test_model(self, x_data, y_data): """Calculate the model's accuracy on the input dataset""" score = self.model.evaluate(x_data, y_data, verbose=0) accuracy = 100 * score[1] return accuracy def train_model(self, x_train, y_train, x_val, y_val): """Train and save the model""" early_stopping = EarlyStopping(monitor='val_loss', patience=sounds_config.patience, mode='min') checkpointer = ModelCheckpoint(filepath=f'{sounds_config.sounds_model_dir}/{self.model.name}.hdf5', verbose=1, save_best_only=True) history = self.model.fit(x_train, y_train, batch_size=sounds_config.num_batch_size, epochs=sounds_config.num_epochs, validation_data=(x_val, y_val), callbacks=[checkpointer, early_stopping], verbose=1) self.le.save(self.model.name) return history def calculate_confusion_matrix(self, x_test, y_test): """Calculate the probabilities required for the confusion matrix and create a dataframe""" y_pred = self.model.predict_classes(x_test) y_test = argmax(y_test, axis=1) con_mat = confusion_matrix(labels=y_test, predictions=y_pred).numpy() con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2) classes = self.le.inverse_transform(list(range(0, self.le.encoded_labels.shape[1]))) return pd.DataFrame(con_mat_norm, index=classes, columns=classes)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=10) input_dim = X_train.shape[1] X_train = np.expand_dims(X_train, axis=2) X_test = np.expand_dims(X_test, axis=2) model = Sequential() model.add(Conv1D(16, kernel_size=3, input_shape=(X_train.shape[1],1), activation='sigmoid')) model.add(MaxPooling1D(pool_size=2)) model.add(Conv1D(8, kernel_size=3, activation='sigmoid')) model.add(MaxPooling1D(pool_size=2)) model.add(Conv1D(4, kernel_size=3, activation='sigmoid')) model.add(MaxPooling1D(pool_size=2)) model.add(Flatten()) model.add(Dense(50, activation='sigmoid')) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) history = model.fit( X_train, y_train, epochs=2000, batch_size=64, validation_split=0.1, verbose=0 ) y_pred = model.predict_classes(X_test) print('Classification Report:') print(classification_report(y_test, y_pred))
# callbacks=callbacks, class_weight={0: 0.25, 1: 0.75}, verbose=verbose) history = model.fit(Xtrain, ytrain, epochs=50, batch_size=256, validation_data=val_data, callbacks=callbacks, class_weight={ 0: 0.25, 1: 0.75 }, verbose=verbose) if kaggle_mode: y_pred = model.predict_classes(Xval) submission = pd.DataFrame({ 'qid': test_data.qid, 'prediction': y_pred.flatten() }) submission.to_csv('submission.csv', index=False) else: joblib.dump(tokenizer, tokenizer_path) model.save(model_path) with open(history_path, "w") as f: f.write(json.dumps(history.history)) f.write("\n\n") f.write(json.dumps(history.params)) f.write("\n\n") f.write("F1 scores: " + json.dumps(f1_callback.f1_scores))
train_labels.append(0) train_labels = np.array(train_labels) train_samples = np.array(train_samples) test_labels = np.array(test_labels) test_samples = np.array(test_samples) scaler = MinMaxScaler(feature_range=(0, 1)) scaled_train_samples = scaler.fit_transform((train_samples).reshape(-1, 1)) scaled_test_samples = scaler.fit_transform((test_samples).reshape(-1, 1)) model = Sequential([ Dense(16, input_shape=(1, ), activation='relu'), Dense(32, activation='relu'), Dense(2, activation='softmax') ]) model.summary() tf.losses.MeanSquaredError model.compile(Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(scaled_train_samples, train_labels, batch_size=20, epochs=10, validation_split=0.2, shuffle=True, verbose=2) predictions = model.predict(scaled_test_samples, batch_size=10, verbose=0) predictions = model.predict_classes(scaled_test_samples, batch_size=10, verbose=0) print(predictions)
img = image.load_img(img_path, target_size=(28, 28)) img_tensor = image.img_to_array(img) img_tensor = np.expand_dims(img_tensor, axis=0) img_tensor /= 255. plt.imshow(img_tensor[0]) plt.show() print(img_tensor.shape) # predicting images x = image.img_to_array(img) x = np.expand_dims(x, axis=0) images = np.vstack([x]) classes = classifier.predict_classes(images, batch_size=10) print("Predicted class is:", classes) # Extracts the outputs of the top 12 layers layer_outputs = [layer.output for layer in classifier.layers[:12]] # Creates a model that will return these outputs, given the model input activation_model = models.Model(inputs=classifier.input, outputs=layer_outputs) # Returns a list of five Numpy arrays: one array per layer activation activations = activation_model.predict(img_tensor) first_layer_activation = activations[0] print(first_layer_activation.shape) plt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis')
model = Sequential([ Dense(16, input_shape=(46, ), activation='relu'), Dense(32, activation='relu'), Dense(3, activation='softmax') ]) model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=10, epochs=15) y_pred = model.predict_classes(x_val) y_pred np.array(list(y_val['relevance'])) from sklearn.metrics import accuracy_score accuracy_score(np.array(list(y_val['relevance'])), y_pred) y_pred_test = model.predict_classes(x_test) x_test.shape y_pred_test.size """### Applying Random Forest with preprocessing"""
model = Sequential([ Dense(16, input_shape=(400, ), activation='relu'), Dense(32, activation='relu'), Dense(2, activation='softmax') ]) model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train_final, batch_size=10, epochs=15) y_val_predict = model.predict_classes(x_val) y_val_predict y_val_final from sklearn.metrics import accuracy_score accuracy_score(y_val_final, y_val_predict) from tensorflow.python.keras.layers import Dense from tensorflow.python.keras import Sequential from tensorflow.python.keras.optimizers import Adam from tensorflow.python.keras.layers import Flatten model_new = Sequential([
# load the dataset dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',') # split into input (X) and output (y) variables X = dataset[:, 0:8] y = dataset[:, 8] # define the keras model model = Sequential() model.add(Dense(12, input_dim=8, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(1, activation='sigmoid')) # compile the keras model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # fit the keras model on the dataset model.fit(X, y, epochs=150, batch_size=10) # evaluate the keras model _, accuracy = model.evaluate(X, y) print('Accuracy: %.2f' % (accuracy * 100)) # make class predictions with the model predictions = model.predict_classes(X) # summarize the first 5 cases for i in range(5): print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))