model = Sequential([ Conv1D(60, 5, padding='causal', activation='relu', input_shape=[None, 1]), Bidirectional(LSTM(60, return_sequences=True)), Bidirectional(LSTM(60)), Dense(30, activation='relu'), Dense(10, activation='relu'), Dense(1), Lambda(lambda x: x * 400) ]) model.compile(loss=Huber(), optimizer=SGD(lr=1e-5), metrics=['mae']) history = model.fit(training_set, epochs=1) predictions = model_predict(temperatures[..., np.newaxis], window_size, batch_size, model) # get index [split-window_size:-1][0] from predictions predictions = predictions[split - window_size:-1, 0] # v_data = np.expand_dims(v_data, axis=-1) print(v_data.shape, predictions.shape) plt.figure(figsize=(10, 6)) plot_series(v_time, v_data) plot_series(v_time, predictions) plt.show() print(tf.keras.metrics.mean_absolute_error(v_data, predictions).numpy()) # print(tf.keras.metrics.mean_absolute_error(list(v_data), list(np.ravel(predictions))).numpy())
model.add(LSTM(s, return_sequences=True)) model.add(Dropout(0.2)) model.add(BatchNormalization()) model.add(LSTM(s, return_sequences=False)) model.add(Dropout(0.2)) model.add(BatchNormalization()) model.add(Dense(32, activation='relu')) model.add(Dropout(0.2)) model.add(BatchNormalization()) model.add(Dense(1)) # Set model params and training rates opt = l model.compile(loss="mse", optimizer=opt, metrics=['mae']) name = f"LSTM-SEQ{SEQUENCE_LENGTH}-Layers{layer}-Size{s}-RMSprop-Regression-{int(time.time())}" tensorboard = TensorBoard(log_dir=f"logs/{name}") # Train model model.fit( trainX, trainY, epochs=5, validation_data=(testX, testY), batch_size=32, callbacks=[tensorboard], )
df = df[[ 'Adj_Close', 'PCT_Change', 'PCT_Change_Low', 'PCT_Change_High', 'Future_Adj_Close' ]] X = df.drop('Future_Adj_Close', axis=1) y = df[['Future_Adj_Close']] X = preprocessing.scale(X) y = preprocessing.scale(y) y = np.array(y) #Tensorboard setup log_dir = "performance/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard = TensorBoard(log_dir=log_dir, histogram_freq=1) #model split train,test, and fitting x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2) model = Sequential() model.add(Dense(1, input_shape=(4, ), activation='linear')) opt = tf.keras.optimizers.Adam(lr=0.03) loss = tf.keras.losses.MSE model.compile(optimizer=opt, loss=loss, metrics=['MSE']) model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test), callbacks=[tensorboard])
#build LSTM model model = Sequential() model.add(LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1))) model.add(LSTM(50, return_sequences=False)) model.add(Dense(25)) model.add(Dense(1)) #compile the model model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy']) #train the model model.fit(x_train, y_train, batch_size=1, epochs=5) #create test dataset test_data = scaled_data[train_data_len - 60:, :] #create dataset x_test, y_test x_test = [] y_test = dataset[train_data_len:, :] for i in range(60, len(test_data)): x_test.append(test_data[i - 60:i, 0]) #convert data to numpy array x_test = np.array(x_test) #reshape the data x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
def create_model(): movies_csv = pd.read_csv('movies.csv') rating_csv = pd.read_csv('ratings.csv') combined_df = pd.merge(movies_csv, rating_csv, on='movieId', how='inner') combined_df = combined_df[['userId', 'title', 'genres', 'rating']] categories = combined_df['genres'].str.split('|') categories_raw = set(combined_df['genres'].tolist()) user_and_movies = [] for i in combined_df.values: user_string = 'user' + str(i[0]) title = re.sub(r"\(.*\)", "", i[1]) user_and_movies.append(user_string + " " + title) all_genre = set() for category in categories: for i in category: cleared_genre = i.translate( str.maketrans('', '', string.punctuation)).lower() all_genre.add(cleared_genre) all_genre = np.array(sorted(list(all_genre))) label_encoder = LabelEncoder() integer_encoded = label_encoder.fit_transform(all_genre) onehot_encoder = OneHotEncoder(sparse=False) integer_encoded = integer_encoded.reshape(len(integer_encoded), 1) onehot_encoded = onehot_encoder.fit_transform(integer_encoded) genre_dict = dict() for i in range(0, len(all_genre)): genre_dict[all_genre[i]] = list(onehot_encoded[i]) onehot_dict = dict() for i in categories_raw: splitted = i.split('|') bitwise_or_genre = [] bitwise_or_genre = np.zeros(len(all_genre), int) for j in splitted: j = j.lower().translate(str.maketrans('', '', string.punctuation)) one_hot_code = genre_dict[j] one_hot_code = np.array(one_hot_code, int) bitwise_or_genre = np.bitwise_or(bitwise_or_genre, one_hot_code) onehot_dict[i] = list(bitwise_or_genre) words_list = [] words = set() for sentece in user_and_movies: sen = '' words_in_sentence = sentece.split() for i in words_in_sentence: if (len(i) > 1): cleared_word = i.lower().translate( str.maketrans('', '', string.punctuation)) words.add(cleared_word) sen = sen + ' ' + cleared_word words_list.append(sen) words = list(words) tokenizer = Tokenizer(num_words=len(words)) tokenizer.fit_on_texts(words_list) words_to_token_dict = tokenizer.word_index for key, value in words_to_token_dict.items(): words_to_token_dict[key] = value + 1 if not os.path.exists('model_folder'): print('\nBuilding Model. Please wait...') title_vectors = [] for word in words_list: word_token = convert_text(word, words_to_token_dict) title_vectors.append(word_token) final_vectors = [] for i in range(0, len(combined_df)): final_vectors.append(title_vectors[i]) genres = categories[i] bitwise_or_genre = [] bitwise_or_genre = np.zeros(len(all_genre), int) if len(genres) > 1: for j in genres: j = j.lower().translate( str.maketrans('', '', string.punctuation)) one_hot_code = genre_dict[j] one_hot_code = np.array(one_hot_code, int) bitwise_or_genre = np.bitwise_or(bitwise_or_genre, one_hot_code) bitwise_or_genre = list(bitwise_or_genre) final_vectors[i].extend(bitwise_or_genre) else: one_hot_code = list( np.array( genre_dict[genres[0].lower().translate( str.maketrans('', '', string.punctuation))], int)) final_vectors[i].extend(one_hot_code) x_dataset = tf.keras.preprocessing.sequence.pad_sequences( final_vectors, padding='post') max_token = words_to_token_dict[max(words_to_token_dict, key=words_to_token_dict.get)] x_dataset = np.asarray(x_dataset) combined_df['rating'] = combined_df['rating'].astype(np.float32) combined_df['rating'] = combined_df['rating'].apply( lambda x: x / 5).values # setting ratings from 0 to 1 y_dataset = combined_df['rating'].values train_indices = int(0.9 * rating_csv.shape[0]) x_train, x_val, y_train, y_val = ( x_dataset[:train_indices], x_dataset[train_indices:], y_dataset[:train_indices], y_dataset[train_indices:], ) embedding_dim = 35 model = Sequential([ Embedding(max_token + 1, embedding_dim, name="embedding"), GlobalAveragePooling1D(), Dense(16, activation='relu'), Dense(1) ]) model.compile(optimizer='sgd', loss='mse', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=256, validation_data=(x_val, y_val), epochs=5, verbose=0) model.save('model_folder') return words_to_token_dict, onehot_dict, model else: model = keras.models.load_model("model_folder") return words_to_token_dict, onehot_dict, model
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf from tensorflow.keras import datasets, models, layers, Sequential import numpy as np import matplotlib.pyplot as plt a = time.time() print(f'Imports complete in {a-b} seconds') (X_train, y_train), (X_test, y_test) = datasets.mnist.load_data() X_train = (X_train / 255).reshape(60000, 28 * 28) X_test = (X_test / 255).reshape(10000, 28 * 28) encoder = Sequential([ layers.Dense(100, activation='relu', input_shape=(28 * 28, )), layers.Dense(50, activation='relu'), layers.Dense(10, activation='linear') ]) decoder = Sequential([ layers.Dense(50, activation='relu'), layers.Dense(500, activation='relu'), layers.Dense(28 * 28, activation='relu') ]) model = Sequential([encoder, decoder]) enc = encoder dec = decoder model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) model.fit(X_train, X_train, epochs=1)
class Generator: def __init__(self,current_path, dataset_name, encode,n_smiles, vocab, max_len, n_layers, units, learning_rate, dropout_rate, activation, epochs, batch_size, optimizer, sampling_t): self.sampling_temp = sampling_t self.model = None self.encode = encode self.vocab = vocab self.vocab_size = vocab.vocab_size self.emb_dim = int(units/2) self.max_len = max_len self.n_layers = n_layers self.units = units self.learning_rate = learning_rate #self.use_dropout = use_dropout self.dropout_rate = dropout_rate self.activation = activation self.epochs = epochs self.batch_size = batch_size if optimizer == 'adam': self.optimizer = optimizer elif optimizer =='adam_clip': self.optimizer = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False, clipvalue=3) elif optimizer == 'SGD': self.optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.0, nesterov=False, name='SGD') elif optimizer == 'RMSprop': self.optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001, rho=0.9, momentum=0.0, epsilon=1e-07, centered=False, name='RMSprop') self.build() folder = "Exp8_Temperature" + "_"+encode+"_"+str(n_smiles)+"_LSTM-"+str(n_layers)+"-"+str(units)+ "-" +str(epochs)+"-"+str(batch_size)+"-"+str(dropout_rate)+"-"+optimizer+ "-"+str(self.emb_dim)+"-"+str(self.sampling_temp)+"\\" self.path = current_path +"\\"+ folder if os.path.exists(self.path): pass else: os.makedirs(self.path) def build(self): self.model = Sequential() if self.encode == 'OHE': self.model.add(Input(shape = (self.max_len, self.vocab_size))) elif self.encode == 'embedding': self.model.add(Embedding(self.vocab_size, self.emb_dim, input_length = self.max_len)) for i in range(self.n_layers): self.model.add(LSTM(self.units, return_sequences=True)) if self.dropout_rate != 0: self.model.add(Dropout(self.dropout_rate)) # for i in range(self.n_layers): # self.model.add(GRU(self.units, return_sequences=True)) # if self.dropout_rate != 0: # self.model.add(Dropout(self.dropout_rate)) # for i in range(self.n_layers): # self.model.add(Bidirectional(LSTM(self.units//2, return_sequences=True))) # if self.dropout_rate != 0: # self.model.add(Dropout(self.dropout_rate)) self.model.add(Dense(units = self.vocab_size, activation = self.activation)) print(self.model.summary()) #compile the model if self.encode == 'OHE': self.model.compile(optimizer = self.optimizer, loss = 'categorical_crossentropy') #OHE elif self.encode == 'embedding': self.model.compile(optimizer = self.optimizer, loss = 'sparse_categorical_crossentropy') #'mse' emb def load_model(self, path): self.model.load_weights(path) def fit_model(self, dataX, dataY): filename="weights-improvement-{epoch:02d}-{loss:.4f}.hdf5" early_stop = EarlyStopping(monitor = "loss", patience=5) path = self.path+F"{filename}" checkpoint = ModelCheckpoint(path, monitor = 'loss', verbose = 1, mode = 'min') callbacks_list = [checkpoint, early_stop]#, PlotLossesKerasTF()] results = self.model.fit(dataX, dataY, verbose = 1, epochs = self.epochs, batch_size = self.batch_size, shuffle = True, callbacks = callbacks_list) #plot fig, ax = plt.subplots() ax.plot(results.history['loss']) ax.set(xlabel='epochs', ylabel = 'loss') figure_path = self.path + "Loss_plot.png" fig.savefig(figure_path) #plt.show() last_epoch = early_stop.stopped_epoch return results, last_epoch def sample_with_temp(self, preds): """ #samples an index from a probability array 'preds' preds: probabilities of choosing a character """ preds_ = np.log(preds).astype('float64')/self.sampling_temp probs= np.exp(preds_)/np.sum(np.exp(preds_)) #out = np.random.choice(len(preds), p = probs) out=np.argmax(np.random.multinomial(1,probs, 1)) return out def generate(self, start_idx, numb, end_idx): """ Generates new SMILES strings, token by token Parameters ---------- start_idx : TYPE int DESCRIPTION. starting index, usually the one that corresponds to 'O' numb : TYPE DESCRIPTION. number of SMILES strings to be generated Returns ------- list_seq : TYPE list of list DESCRIPTION. A list where each entry is a tokenized SMILES """ list_seq = [] if self.encode == 'embedding': for j in tqdm(range(numb)): seq = [start_idx] #x = np.reshape(seq, (1, len(seq),1)) for i in range(self.max_len-1): x = np.reshape(seq, (1, len(seq),1)) preds = self.predict(x) #sample #index = np.argmax(preds[0][-1]) #sample with T index = self.sample_with_temp(preds[0][-1]) seq.append(index) if (index) == end_idx: break list_seq.append(seq) elif self.encode =='OHE': for j in tqdm(range(numb)): start_idx_oh = np.zeros(self.vocab_size, dtype = np.int8) start_idx_oh[start_idx] = 1 seq = [start_idx_oh] for i in range(self.max_len-1): x = np.reshape(seq, (1, len(seq), self.vocab_size)) preds = self.predict(x) index = self.sample_with_temp(preds[0][-1]) aux = np.zeros(self.vocab_size, dtype = np.int8) aux[index] = 1 seq.append(aux) if (index) == end_idx: break list_seq.append(seq) return list_seq def predict(self, input_x): preds = self.model.predict(input_x, verbose=1) return preds
test_size = len(data_y) - train_size test_x = np.array(data_x[train_size : len(data_x)]) test_y = np.array(data_y[train_size : len(data_y)]) # 모델 생성 model = Sequential() model.add(LSTM(units=10, activation='relu', return_sequences=True, input_shape=(window_size, data_size))) model.add(Dropout(0.1)) model.add(LSTM(units=10, activation='relu')) model.add(Dropout(0.1)) model.add(Dense(units=1)) model.summary() model.compile(optimizer='adam', loss='mean_squared_error') model.fit(train_x, train_y, epochs=60, batch_size=30) pred_y = model.predict(test_x) # Visualising the results plt.figure() plt.plot(test_y, color='red', label='real SEC stock price') plt.plot(pred_y, color='blue', label='predicted SEC stock price') plt.title('SEC stock price prediction') plt.xlabel('time') plt.ylabel('stock price') plt.legend() plt.show() # raw_df.close[-1] : dfy.close[-1] = x : pred_y[-1] print("Tomorrow's SEC price :", raw_df.close[-1] * pred_y[-1] / dfy.close[-1], 'KRW')
class C3d_Model(): def __init__(self, learning_rate=0.0001, num_features=4096, L=16): self.model = Sequential() # reshape the input images from 224*224 into 112*112 self.model.add(AveragePooling3D((1, 2, 2), strides=(1, 2, 2))) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(L, 112, 112, 3))) self.model.add(Conv3D(64, (3, 3, 3), activation='relu', name='conv1a')) self.model.add(MaxPooling3D((1, 2, 2), strides=(1, 2, 2))) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(L, 56, 56, 3))) self.model.add(Conv3D(128, (3, 3, 3), activation='relu', name='conv2a')) self.model.add(MaxPooling3D((2, 2, 2), strides=(2, 2, 2))) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(8, 28, 28, 3))) self.model.add(Conv3D(256, (3, 3, 3), activation='relu', name='conv3a')) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(8, 28, 28, 3))) self.model.add(Conv3D(256, (3, 3, 3), activation='relu', name='conv3b')) self.model.add(MaxPooling3D((2, 2, 2), strides=(2, 2, 2))) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(4, 14, 14, 3))) self.model.add(Conv3D(512, (3, 3, 3), activation='relu', name='conv4a')) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(4, 14, 14, 3))) self.model.add(Conv3D(512, (3, 3, 3), activation='relu', name='conv4b')) self.model.add(MaxPooling3D((2, 2, 2), strides=(2, 2, 2))) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(2, 7, 7, 3))) self.model.add(Conv3D(512, (3, 3, 3), activation='relu', name='conv5a')) self.model.add(ZeroPadding3D((1, 1, 1), input_shape=(2, 7, 7, 3))) self.model.add(Conv3D(512, (3, 3, 3), activation='relu', name='conv5b')) self.model.add(MaxPooling3D((2, 2, 2), strides=(2, 2, 2))) self.model.add(Flatten()) self.model.add( Dense(num_features, name='fc6', kernel_initializer='glorot_uniform')) self.model.add( BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001)) self.model.add(Activation('relu')) self.model.add(Dropout(0.1)) self.model.add( Dense(num_features, name='fc7', kernel_initializer='glorot_uniform')) self.model.add( BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001)) self.model.add(Activation('relu')) self.model.add(Dropout(0.2)) self.model.add( Dense(1, name='predictions', kernel_initializer='glorot_uniform')) self.model.add(Activation('sigmoid')) adam = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08) self.model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy']) # self.model.build((None,L,224,224,3)) def summary(self): self.model.summary() # plot the structure of VGG model def get_model(self): return self.model def train(self, train_generator, valid_generator, train_file, valid_file, batch_size, callbacks_list, epoch=50, verbose=2): self.model.fit(train_generator, steps_per_epoch=len(train_file) // batch_size, epochs=epoch, validation_data=valid_generator, validation_steps=len(valid_file) // batch_size, callbacks=callbacks_list, verbose=2) def load_weights(self, h5_path): self.model.load_weights(r'%s' % h5_path) def test(self, test_generator, steps, verbose=2): self.model.evaluate_generator(test_generator, steps=steps, verbose=verbose)
import tensorflow as tf import numpy as np from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense L0 = Dense(units=1, input_shape=[1]) model = Sequential([L0]) model.compile(optimizer='sgd', loss='mean_squared_error') xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float) ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float) model.fit(xs, ys, epochs=500) print(model.predict([10.0])) print("Here is what i've learned: {}".format(L0.get_weights())) export_dir = 'saved_model/1' tf.saved_model.save(model, export_dir)
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' print(tf.__version__) print(keras.__version__) df = pd.read_csv("./data.csv", header=None) traget_cat = pd.Categorical(df.iloc[:, -1]) df.iloc[:, -1] = traget_cat.codes X_VAL = df.values[:, :-1] X_TAR = df.values[:, -1] model = Sequential() model.add(keras.layers.Flatten(input_shape=(35, ))) model.add(keras.layers.Dense(50, activation="relu")) model.add(keras.layers.Dense(20, activation="relu")) model.add(keras.layers.Dense(10, activation="softmax")) print(model.summary()) model.compile(optimizer='sgd', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(X_VAL, X_TAR, epochs=10) model.save('aut_model.h5') # result = model.predict_classes(X_VAL[4:6]) # print(result)
optimizer="adam", metrics=["accuracy"]) epochs = 40 batch_size = 50017 # - # To estimate errors from predicted values we get from the model, we define error function as binary_crossentropy. To reduce this error, we use the optimizer adam (or sgd, rmsprop, etc.). In this step the model transforms from a blueprint to a concrete object (think of how re.compile becomes an object when applied). It also automatically adds bias at this step (by deafult its xavier methodology of initializing the weights). # # Epochs is one full read of the training dataset to estimate the weights based on the error, i.e. one round of forward and backward propagation to minimize the error. We cannot decide the number of epochs before hand, but there is method to prevent wastage of computational resource called 'early stopping'. # # To prevent outliers from skewing our data, do an unsupervised clustering to see these outliers. Also, hierarchical clustering. history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.3, verbose=True) loss, accuracy = model.evaluate(X_test, y_test, verbose=False) model.summary() # + print(history.history["accuracy"]) print(history.history["val_accuracy"]) ta = pd.DataFrame(history.history["accuracy"]) va = pd.DataFrame(history.history["val_accuracy"]) tva = pd.concat([ta, va], axis=1)
def main(): numpy.random.seed(7) # data. definition of the problem. seq_length = 20 x_train, y_train = task_add_two_numbers_after_delimiter(20_000, seq_length) x_val, y_val = task_add_two_numbers_after_delimiter(4_000, seq_length) print(x_train.shape) print(y_train.shape) print(x_val.shape) print(y_val.shape) # just arbitrary values. it's for visual purposes. easy to see than random values. test_index_1 = 4 test_index_2 = 9 x_test, _ = task_add_two_numbers_after_delimiter(10, seq_length, 0, test_index_1, test_index_2) # x_test_mask is just a mask that, if applied to x_test, would still contain the information to solve the problem. # we expect the attention map to look like this mask. x_test_mask = np.zeros_like(x_test[..., 0]) x_test_mask[:, test_index_1:test_index_1 + 1] = 1 x_test_mask[:, test_index_2:test_index_2 + 1] = 1 model = Sequential([ Bidirectional(LSTM(100, return_sequences=True), input_shape=(seq_length, 1)), Bidirectional(LSTM(100, return_sequences=True)), Bidirectional(LSTM(100, return_sequences=True)), Bidirectional(LSTM(100, return_sequences=True)), Attention(name='attention_weight'), Dropout(0.2), Dense(1, activation='linear') ]) model.compile(loss='mse', optimizer='adam') print(model.summary()) output_dir = 'task_add_two_numbers' if not os.path.exists(output_dir): os.makedirs(output_dir) max_epoch = int(sys.argv[1]) if len(sys.argv) > 1 else 3 # class VisualiseAttentionMap(Callback): # def on_epoch_end(self, epoch, logs=None): # attention_map = get_activations(model, x_test, layer_names='attention_weight')['attention_weight'] # # top is attention map. # # bottom is ground truth. # plt.imshow(np.concatenate([attention_map, x_test_mask]), cmap='hot') # iteration_no = str(epoch).zfill(3) # plt.axis('off') # plt.title(f'Iteration {iteration_no} / {max_epoch}') # plt.savefig(f'{output_dir}/epoch_{iteration_no}.png') # plt.close() # plt.clf() history = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=max_epoch, batch_size=64) print(history)
FalseNegatives(), TruePositives(), TrueNegatives(), Recall(), Precision() ]) print(gru_model.summary()) if 'resample' not in AUGMENT_METHOD: class_weight = {0: 1., 1: 69.} else: class_weight = {0: 1., 1: 1.} gru_model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch, validation_data=(X_test_scaled, y_test), validation_freq=100, verbose=2, class_weight=class_weight) scores = gru_model.evaluate(X_test_scaled, y_test, verbose=1) print(scores) # Save the model gru_model.save('final_model.ker') X = np.array(X) X = X.reshape(X.shape[0], LOOKBACK, n_features) final_scaler = StandardScaler().fit(flatten(X)) with open('final_scaler.pickle', 'wb') as wf: pickle.dump(final_scaler, wf)
def LSTM(self, lookback=7, row='Total', num_estimators=10): daily_df = self.daily_df.copy() scaler = MinMaxScaler() train = daily_df[row].tolist() train = np.array(train).reshape((-1, 1)) train = scaler.fit_transform(train) train = train.ravel() x_train, y_train = [], [] for i in range(len(train) - lookback - self.forecast_interval + 1): x = train[i:i + lookback] x_train.append(x) y_train.append(train[i + lookback:i + lookback + self.forecast_interval]) train_pred = [] for i in range(len(train) - lookback + 1): train_pred.append(train[i:i + lookback]) train_pred = np.array(x_train) train_pred = train_pred.reshape((train_pred.shape[0], train_pred.shape[1], 1)) x_train, y_train = np.array(x_train), np.array(y_train) x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], 1)) print(x_train.shape) callback = tensorflow.keras.callbacks.EarlyStopping(monitor='loss', patience=5) y_hat = [] model = Sequential() model.add(LSTM(50, input_shape=(lookback, 1))) # model.add(LSTM(50, activation='relu')) # model.add(Dropout(0.2)) model.add(Dense(self.forecast_interval)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(x_train, y_train, epochs=100, verbose=1, shuffle=True, callbacks=[callback]) x_test = np.array(y_train[-1]).reshape((1, x_train.shape[1], 1)) y_hat.append(scaler.inverse_transform(model.predict(train_pred))) y_hat = np.array(y_hat) y_hat = y_hat.reshape(-1, 7) latest_pred = np.array(scaler.inverse_transform(model.predict(y_train[-1].reshape((1, y_train.shape[1], 1))))) ans = [] for i in range(y_hat.shape[0]): temp = np.concatenate((np.array([np.nan] * i), y_hat[i], np.array([np.nan] * (y_hat.shape[0] - i + self.forecast_interval - 1))), axis=0) ans.append(temp) ans.append(np.concatenate((np.array([np.nan] * (y_hat.shape[0] + self.forecast_interval - 1)), latest_pred[0]), axis=0)) ans = np.array(ans) print(ans.shape) low, high = [], [] pred = [] for i in range(ans.shape[1]): low.append(max(np.nanmin(ans[:, i]), 0)) high.append(np.nanmax(ans[:, i])) pred.append(max(np.nanmean(ans[:, i]), 0)) forecasted_days = self.generate_dates(daily_df.index[-1], self.forecast_interval) preds = pd.DataFrame(data=pred, columns=["forecast"], index=daily_df.index[lookback:].union(forecasted_days)) interval_low = pd.DataFrame(data=low, columns=["interval_low"], index=daily_df.index[lookback:].union(forecasted_days)) interval_high = pd.DataFrame(data=high, columns=["interval_high"], index=daily_df.index[lookback:].union(forecasted_days)) daily_df = pd.concat([daily_df, preds, interval_low, interval_high], axis=1) return daily_df
model.add(layers.Dropout(0.25)) model.add(layers.Dense(np.unique(target).size * 4, activation='relu')) model.add(layers.Dropout(0.25)) model.add(layers.Dense(np.unique(target).size, activation='softmax')) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.summary() # training TF model ITERATION = 2000 BATCH_SIZE = 16 history = model.fit(data_train, target_train, epochs=ITERATION, batch_size=BATCH_SIZE, validation_data=(data_validate, target_validate)) predictions = model.predict(data_test) test_score = model.evaluate(data_test, target_test) # get the predicted label based on probability predictions_categorical = np.argmax(predictions, axis=1) # display prediction performance on validation data and test data print('Prediction Accuracy:', accuracy_score(target_test, predictions_categorical).round(3)) print('Test accuracy:', round(test_score[1], 3)) print('Test loss:', round(test_score[0], 3)) print('')
# 3. keras model model = Sequential() # 4. DNN model layer 구축 model.add(Dense(32, input_shape=(64, ), activation="relu")) model.add(Dense(16, activation="relu")) model.add(Dense(10, activation="softmax")) # 5. model compile : 학습과정 설정(다항 분류기) model.compile( optimizer='adam', # 최적화 알고리즘(lr 생략) loss='categorical_crossentropy', # 손실 metrics=['accuracy']) model.summary() # 6. model training model.fit(x_train, y_train, epochs=100, verbose=1, validation_data=(x_val, y_val)) # 7. model evaluation : test dataset model.evaluate(x_val, y_val) # 8. model save : file save - HDF5 파일 형식 model.save(filepath="keras_model_digits.h5") print("model saved...")
input_dim = X_train.shape[1:] output_dim = y.shape[1] print('input_dim', input_dim) print('output_dim', output_dim) # create and train network # you can customize the layers as you prefer nn = Sequential() nn.add(layers.Dense(units=50, activation='relu', input_shape=input_dim)) nn.add(layers.Dense(units=50, activation='relu')) nn.add(layers.Dense(output_dim, activation='softmax')) # use categorical_crossentropy for multi-class classification nn.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) nn.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=100, verbose=0) print('Accuracy: %.1f' % nn.evaluate(X_test, y_test)[1]) # export to file with open('wine_nn.h', 'w', encoding='utf-8') as file: print( port(nn, variable_name='wine_model', pretty_print=True, optimize=False))
f'model.hd5', verbose=1, save_best_only= True, save_weights_only= True ) optimizer = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999 ) model.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics=['accuracy']) print('------------------------------------------------------------------------') print(f'Training for fold {fold_no} ...') history = model.fit(TRAIN_IMAGES,TEST_IMAGES, batch_size=128, epochs=10, validation_data=(TRAIN_VAL,TEST_VAL), # validation_steps = TRAIN_VAL.shape[0] // 64, verbose=1, callbacks = [early_stopping_callback, ckpt]) scores = model.evaluate(inputs[test], targets[test], verbose=0) print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%') acc_per_fold.append(scores[1] * 100) loss_per_fold.append(scores[0]) # Increase fold number fold_no = fold_no + 1 !pip install visualkeras import visualkeras
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33) print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) # determine the number of input features n_features = X_train.shape[1] # define model model = Sequential() model.add( Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features, ))) model.add(Dense(8, activation='relu', kernel_initializer='he_normal')) model.add(Dense(1, activation='sigmoid')) # compile the model model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # fit the model model.fit(X_train, y_train, epochs=150, batch_size=32, verbose=0) # evaluate the model loss, acc = model.evaluate(X_test, y_test, verbose=0) print('Test Accuracy: %.3f' % acc) # make a prediction row = [ 1, 0, 0.99539, -0.05889, 0.85243, 0.02306, 0.83398, -0.37708, 1, 0.03760, 0.85243, -0.17755, 0.59755, -0.44945, 0.60536, -0.38223, 0.84356, -0.38542, 0.58212, -0.32192, 0.56971, -0.29674, 0.36946, -0.47357, 0.56811, -0.51171, 0.41078, -0.46168, 0.21266, -0.34090, 0.42267, -0.54487, 0.18641, -0.45300 ] yhat = model.predict([row]) print('Predicted: %.3f' % yhat)
x = tf.nn.relu(x) x = self.fc3(x) x = tf.nn.relu(x) x = self.fc4(x) x = tf.nn.relu(x) x = self.fc5(x) return x network = MyModel() network.compile(optimizer=optimizers.Adam(lr=0.01), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy']) network.fit(db, epochs=5, validation_data=ds_val, validation_freq=2) network.evaluate(ds_val) sample = next(iter(ds_val)) x = sample[0] y = sample[1] # one-hot pred = network.predict(x) # [b, 10] # convert back to number y = tf.argmax(y, axis=1) pred = tf.argmax(pred, axis=1) print(pred) print(y)
##### Training ############ model = Sequential([ Dense(256, input_shape=X_train[0].shape), Activation('relu'), Dropout(0.5), Dense(256), Activation('relu'), Dropout(0.5), Dense(2, activation='softmax') ]) print(model.summary()) model.compile(loss="categorical_crossentropy", optimizer='adam', metrics=['accuracy']) print("Model Score: \n") history = model.fit(X_train, y_train, epochs=1000) model.save("saved_model/WWD.h5") score = model.evaluate(X_test, y_test) print(score) #### Evaluating our model ########### print("Model Classification Report: \n") y_pred = np.argmax(model.predict(X_test), axis=1) cm = confusion_matrix(np.argmax(y_test, axis=1), y_pred) print(classification_report(np.argmax(y_test, axis=1), y_pred)) plot_confusion_matrix(cm, classes=["Does not have Wake Word", "Has Wake Word"])
class Model: def __init__(self, name='model'): self.model = None self.classes = None self.train_data = None self.features_ordered = None self.model_pt = os.path.join(params.root_dir, name) def __get_extensions_str(self, files): if files is not None: if type(files) is str: files = json.loads(files.replace('\'', "\"")) extensions = list( filter( lambda x: len(x) > 0, map( lambda x: x.split('.')[-1] if '.' in x else 'noextension', files))) extensions = ' '.join(extensions).lower() else: extensions = '' return extensions def __get_sender_str(self, sender): if sender is None: return '' sender = sender.lower() return ' '.join( filter(lambda x: len(x) > 0, re.split(r'[^a-z]', sender))) def __get_unsubscribe_str(self, unsubscribe): if unsubscribe is None: return '' return 'false' if int(unsubscribe) == 0 else 'true' def build_data(self, classes, data_path): self.classes = sorted(list(map(lambda l: l.lower(), classes))) data = pd.read_csv(data_path).replace({np.nan: None}) items = [] for i, row in data.iterrows(): label = row['Type'].lower() if label not in self.classes: continue sender = self.__get_sender_str(row['Sender']) subject = util.clean(row['Subject']) try: text = util.clean(row['Text']) except: print(row) raise Exception unsubscribe = self.__get_unsubscribe_str(row['Unsubscribe']) extensions = self.__get_extensions_str(row['Files']) items.append({ 'sender': sender, 'subject': subject, 'text': text, 'unsubscribe': unsubscribe, 'extensions': extensions, 'type': label }) self.train_data = pd.DataFrame(items) self.train_data.to_csv(os.path.join(os.path.dirname(data_path), 'train.csv'), index=False) def train(self, vocab_size=None, split_ratio=0.9, num_epochs=5): if self.classes is None: print( 'Classes list is none, did you use parse method before calling train?' ) return if self.train_data is None: print( 'Train dataframe is none, did you use parse method before calling train?' ) return # ----------- convert train df to numpy array for X and Y ----------- train_test_data = [] self.features_ordered = [ 'unsubscribe', 'extensions', 'sender', 'subject', 'text' ] for i, row in self.train_data.iterrows(): x = '' for col in self.features_ordered: if row[col] is not None and len(row[col]) > 0: x += str(row[col]).lower() + ' ' x = x.strip() idx = self.classes.index(row['type']) if idx == -1: continue y = np.zeros(len(self.classes)) y[idx] = 1 train_test_data.append((x, y)) train_test_data = np.array(train_test_data) # ----------- split to train x, y; test x, y----------- idx = int(len(train_test_data) * split_ratio) np.random.shuffle(train_test_data) train = train_test_data[:idx] test = train_test_data[idx:] train_x = np.array([i[0] for i in train]) train_y = np.array([i[1] for i in train]) test_x = np.array([i[0] for i in test]) test_y = np.array([i[1] for i in test]) # -------- build model -------- encoder = TextVectorization(max_tokens=vocab_size) encoder.adapt(train_x) self.model = Sequential([ encoder, Embedding(input_dim=len(encoder.get_vocabulary()), output_dim=64, mask_zero=True), Bidirectional(LSTM(64)), Dense(64, activation='relu'), Dense(len(self.classes), activation='softmax') ]) self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # -------- train model -------- self.model.fit(x=train_x, y=train_y, batch_size=64, epochs=num_epochs, validation_data=(test_x, test_y)) def load_model(self): self.model = load_model(self.model_pt) with open(os.path.join(self.model_pt, 'classes.pickle'), 'rb') as fp: self.classes = pickle.load(fp) with open(os.path.join(self.model_pt, 'features_ordered.pickle'), 'rb') as fp: self.features_ordered = pickle.load(fp) def save_model(self): self.model.save(self.model_pt) with open(os.path.join(self.model_pt, 'classes.pickle'), 'wb') as fp: pickle.dump(self.classes, fp) with open(os.path.join(self.model_pt, 'features_ordered.pickle'), 'wb') as fp: pickle.dump(self.features_ordered, fp) def predict(self, unsubscribe, sender, subject, text, files): item = { 'unsubscribe': self.__get_unsubscribe_str(unsubscribe), 'sender': self.__get_sender_str(sender), 'subject': util.clean(subject) if subject is not None else '', 'text': util.clean(text) if text is not None else '', 'extensions': self.__get_extensions_str(files) } x = '' for col in self.features_ordered: if item[col] is not None and len(item[col]) > 0: x += str(item[col]).lower() + ' ' x = x.strip() predictions = self.model.predict(np.array([x]))[0] return self.classes[np.argmax(predictions)], predictions, x
class MLPregressor(BaseEstimator): def __init__(self, n_in, n_out, epochs, batch_size, lrate, split): self.n_in = n_in self.n_out = n_out self.epochs = epochs self.batch_size = batch_size self.lrate = lrate self.split = split def build( self, layers, ): self.model = Sequential([ Dense(layers[0], use_bias=False, input_shape=(self.n_in, )), BatchNormalization(), ReLU(), Dropout(0.1), Dense(layers[1], use_bias=False), BatchNormalization(), ReLU(), Dropout(0.1), Dense(layers[2], use_bias=False), BatchNormalization(), ReLU(), Dropout(0.1), Dense(layers[3], use_bias=False), BatchNormalization(), ReLU(), Dropout(0.1), Dense(self.n_out) ]) # compile self.model.compile(Adam(lr=self.lrate), loss='mean_absolute_error') print(self.model.summary()) return self.model def fit(self, X_train, y_train): tic = timeit.default_timer() history = self.model.fit(X_train, y_train, batch_size=self.batch_size, epochs=self.epochs, validation_split=self.split, verbose=1) toc = timeit.default_timer() self.fit_time = toc - tic return history def predict(self, Xt, yt): tic = timeit.default_timer() y_hat = pd.DataFrame(self.model.predict(Xt), index=yt.index.values, columns=yt.columns) toc = timeit.default_timer() self.pred_time = toc - tic return y_hat def evaluate(self, y_hat, y_test, results, scoreDict, scores): for var in y_test.columns: if var in ['adj', 'cluster', 'admix']: continue #print (var) y_t = y_test.loc[:, var].astype(float) y_h = y_hat.loc[:, var].astype(float) if scores in ['regScore']: true = np.logical_and(y_h > 0, y_t > 0) y_tst = y_t[true] y_ht = y_h[true] else: y_tst = y_t y_ht = y_h for stat in scoreDict: results[var][stat].append(scoreDict[stat](y_tst, y_ht)) results[var]['ytest'].append(y_tst) results[var]['yhat'].append(y_ht) # results['pred_time'].append(self.pred_time) # results['fit_time'].append(self.fit_time) return results
def CNN_model(): data_aug = ImageDataGenerator(rotation_range=30, zoom_range=0.3, width_shift_range=0.3, height_shift_range=0.3, shear_range=0.3, horizontal_flip=True, fill_mode="nearest") # Sequential Model model = Sequential() # Convolutional Layer 1 model.add(layers.Conv2D(32, 3, activation='relu', input_shape=(32, 32, 3))) model.add(layers.Conv2D(32, 3, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Dropout(0.25)) # Convolutional Layer 2 model.add(layers.Conv2D(64, 3, activation='relu')) model.add(layers.Conv2D(64, 3, activation='relu')) model.add(layers.BatchNormalization()) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Dropout(0.25)) # Flattening model.add(layers.Flatten()) # Fully connected layer model.add(layers.Dense(800, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(400, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(num_classes, activation='softmax')) opt = optimizers.Adam(learning_rate=0.001) # Compiling Model model.compile( optimizer=opt, # how model is updated based on data and loss function loss=tf.keras.losses.SparseCategoricalCrossentropy( ), # measures model accuracy metrics=['accuracy']) # monitor training and testing steps # Training model epochs = 50 CNN_history = model.fit(data_aug.flow(train_img, train_labels, batch_size=batch_size, shuffle=False), epochs=epochs, validation_data=(val_img, val_labels)) # Model Summary model.summary() # Evaluate accuracy test_loss, test_acc = model.evaluate(test_img, test_labels, verbose=2) print(f'\nTest Accuracy: {test_acc}') # Training Plot plot_graph(CNN_history, epochs) ### Making predictions ### predictions = model.predict( test_img ) # array of numbers representing its confidence for each class # Verifying predictions using test images and prediction arrays num_rows = 5 num_cols = 3 num_images = num_rows * num_cols plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows)) for i in range(num_images): plt.subplot(num_rows, 2 * num_cols, 2 * i + 1) plot_image(i, predictions[i], test_labels, test_img) plt.subplot(num_rows, 2 * num_cols, 2 * i + 2) plot_value_array(i, predictions[i], test_labels) plt.tight_layout() # show all plots plt.show() # Saving Model model.save('saved_model/CNN_model')
class CognitiveAgent(Agent): def __init__(self, input_size): """Class constructor Arguments: input_size {integer} -- Size of the input of the neuronal network """ self.model = Sequential() self.model.add(Dense(500, activation='relu', input_dim=(input_size))) self.model.add(Dense(250, activation='relu')) self.model.add(Dense(100, activation='relu')) self.model.add(Dense(1)) self.model.compile(loss='mse', optimizer=SGD(), metrics=['mae', 'mse']) self.early_stop = EarlyStopping(monitor='val_loss', patience=30) self.scalers = [] def load_state(self): """Load a pre-saved agent """ try: self.model.load_weights('states/adhoc_wifi.h5') self.scalers = [ load('states/x_scaler.joblib'), load('states/y_scaler.joblib') ] except: print( "WARNING | Agent states not found, please check the folder 'states'" ) def save_state(self): """Save the learning of the agent and the data scalers """ if len(self.scalers) > 0: dump(self.scalers[0], 'states/x_scaler.joblib') dump(self.scalers[1], 'states/y_scaler.joblib') self.model.save_weights('states/adhoc_wifi.h5') def data_preprocessing(self, X, y): """Function to divide the data in train and test, and normalize it Arguments: X {array} -- Observations y {array} -- Targets Returns: tuple -- Train and test data divied, normalized and mixing """ # Divide data in training and test X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2) # Data normalization scaler = StandardScaler().fit(X_train) X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) y_scaler = StandardScaler().fit(y_train.reshape(-1, 1)) y_train = y_scaler.transform(y_train.reshape(-1, 1)) y_test = y_scaler.transform(y_test.reshape(-1, 1)) self.scalers = [scaler, y_scaler] return (X_train, y_train, X_test, y_test) def learn(self, X, y, validation_data=None, epochs=3): """Function to fit the neuronal network to certain data Arguments: X {array} -- Observations y {array} -- Targets Keyword Arguments: validation_data {tuple} -- Custom validation data, if None is calculated (default: {None}) epochs {int} -- Epochs of the training model (default: {3}) Returns: history -- Learning performance """ if validation_data == None: X_train, X_test, y_train, y_test = self.data_preprocessing(X, y) validation_data = (X_test, y_test) return self.model.fit(X, y, epochs=epochs, validation_data=validation_data, verbose=1, callbacks=[self.early_stop]) def get_action(self, x): """Get an action according to an determined observation Arguments: x {array} -- Sample to get a prediction Returns: integer -- Action to perform """ x = np.array(x).reshape(-1, 3) prediction = self.scalers[1].inverse_transform(self.model.predict(x)) return int(np.round(prediction.reshape(1)[0]))
def train_cnn(path="model/", to_simple_digit=False, epochs=100, ft_epochs=100, learning_rate=0.01): """ Train the CNN model and save it under the given path. The method first loads the models using :py:doc:`generate_datasets.py <training.generate_datasets.py>` methods. Then the model is trained, saved and finally evaluated. Training is run in two steps: It is first trained with synthetic data and then finetuned with real data. Early stopping is used to prevent overfitting. Args: path(str): The directory to save the trained model to. (Default value = "model/") to_simple_digit(bool): If true, convert the datasets to simple 9 + 1 class digit recognition. (Default value = False) epochs(int): The number of epochs. (Default value = 100) ft_epochs: The number of finetuning epochs. (Default value = 100) learning_rate: The learning rate for the Adadelta optimizer. (Default value = 0.01) Returns: None """ os.makedirs(path, exist_ok=True) print("Loading data..") concat_machine, concat_hand, concat_out, real_training, real_validation = load_datasets( TRANSFORMED_DATASET_NAMES) batch_size = 256 train_generator = SimpleDataGenerator(concat_machine.train, concat_hand.train, concat_out.train, batch_size=batch_size, shuffle=True, to_simple_digit=to_simple_digit) dev_generator = SimpleDataGenerator(concat_machine.test, concat_hand.test, concat_out.test, batch_size=batch_size, shuffle=True, to_simple_digit=to_simple_digit) ft_train_generator = SimpleDataGenerator(real_training.train, batch_size=batch_size, shuffle=True, to_simple_digit=to_simple_digit) ft_dev_generator = SimpleDataGenerator(real_training.test, batch_size=batch_size, shuffle=True, to_simple_digit=to_simple_digit) test_generator = SimpleDataGenerator(real_validation.test, batch_size=batch_size, shuffle=False, to_simple_digit=to_simple_digit) # Run training on the GPU with tf.device('/GPU:0'): # Keras Model print("Creating model..") model = Sequential() model.add(layers.InputLayer(input_shape=(28, 28, 1))) model.add(layers.Conv2D(16, (3, 3), padding='same')) # 28x28x16 model.add(layers.BatchNormalization()) model.add(layers.Activation('relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 14x14x16 model.add(layers.Conv2D(32, (3, 3))) # 12x12x32 model.add(layers.BatchNormalization()) model.add(layers.Activation('relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 6x6x32 model.add(layers.Conv2D(64, (3, 3))) # 4x4x64 model.add(layers.BatchNormalization()) model.add(layers.Activation('relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 2x2x64 model.add(layers.Conv2D(128, (3, 3), padding='same')) # 2x2x64 model.add(layers.BatchNormalization()) model.add(layers.Activation('relu')) model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 1x1x128 model.add(layers.Flatten()) # 64 model.add(layers.Dense(128, activation='relu')) model.add(layers.Dropout(0.25)) model.add(layers.Dense(128, activation='relu')) model.add(layers.Dense(train_generator.num_classes)) # Hyperparameters print("Compiling model..") model.compile( loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.Adadelta(learning_rate), metrics=['accuracy']) print(model.summary()) print("Training model on") model.fit(train_generator, validation_data=dev_generator, epochs=epochs, callbacks=[ EarlyStopping(monitor='val_accuracy', restore_best_weights=True, patience=3, min_delta=0.0001), ]) print("Finetuning model") model.fit(ft_train_generator, validation_data=ft_dev_generator, epochs=ft_epochs, callbacks=[ EarlyStopping(monitor='val_accuracy', restore_best_weights=True, patience=3, min_delta=0.0001), ]) print("Saving keras model") models.save_model(model, path + "model.h5") print("Evaluating keras model") print("Training dev", dict(zip(model.metrics_names, model.evaluate(dev_generator)))) print("Finetuning dev", dict(zip(model.metrics_names, model.evaluate(ft_dev_generator)))) print("Test", dict(zip(model.metrics_names, model.evaluate(test_generator)))) evaluate(model, test_generator)
# deal with data train_db = db_train.map(progress).batch(128) test_db = db_test.map(progress).batch(128) train_iter = iter(train_db) train_next = next(train_iter) print(train_next[0].shape) # 利用Tensorflow的Sequential容器去构建model model = Sequential([ # layers.Dense(256, activation=tf.nn.relu), # layers.Dense(128, activation=tf.nn.relu), # layers.Dense(64, activation=tf.nn.relu), # layers.Dense(32, activation=tf.nn.relu), layers.Dense(10) ]) model.build(input_shape=[None, 32*32*3]) model.summary() # 利用Sequential的compile方法,简化损失函数,梯度优化,计算准确率等操作 model.compile(optimizer=optimizers.Adam(lr = 0.01), loss = tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy'] ) # 设置模型对整体数据重复训练5次,每训练2次打印一次正确率 model.fit(train_db, epochs=2, validation_data = test_db, validation_freq=2) # 另一种打印模型正确率的接口方法 model.evaluate(test_db)
32, input_shape=( train_X.shape[1], train_X.shape[2]), return_sequences=True)) model.add(GRU(16, input_shape=(train_X.shape[1], train_X.shape[2]))) model.add(Dense(16, activation="relu")) model.add(Dense(1)) model.compile(loss=tf.keras.losses.Huber(), optimizer='adam', metrics=["mse"]) history = model.fit( train_X, train_y, epochs=50, batch_size=72, validation_data=( test_X, test_y), verbose=2) # 画图 plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='test') plt.legend() plt.show() # 画图 plt.plot(history.history['mse'], label='train') plt.plot(history.history['val_mse'], label='test') plt.legend() plt.show()
model.add(Conv2D(128, (3, 3), activation='relu')) model.add(BatchNormalization()) model.add(MaxPool2D(2, 2)) model.add(Dropout(0.5)) model.add(Flatten()) model.add(Dense(41, activation='softmax')) #編譯驗證訓練集 model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(X_train, y_train, batch_size=64, epochs=50, validation_data=(X_test, y_test)) #get loss value檢查model是否正常 #loss, accuracy = model.evaluate(X_test, y_test, batch_size = 128) #測試print('test ') #測試print('loss %s\n accuracy %s' % (loss, accuracy)) #將訓練好的model存起來 model.save('./CNN_Model.h5') testdata = pd.read_csv('test.csv') Test = [] for i in range(testdata.shape[0]):