def test(settings, sid=9): #sess = K.get_session() K.clear_session() #sess = tf.Session(graph=g) #K.set_session(sess) np.random.seed(sid) tf.random.set_seed(sid) # Model parameter # ---------------------------------------------------------------------------- # | | 200-epoch | Orig Paper| 200-epoch | Orig Paper| sec/epoch # Model | n | ResNet v1 | ResNet v1 | ResNet v2 | ResNet v2 | GTX1080Ti # |v1(v2)| %Accuracy | %Accuracy | %Accuracy | %Accuracy | v1 (v2) # ---------------------------------------------------------------------------- # ResNet20 | 3 (2)| 92.16 | 91.25 | ----- | ----- | 35 (---) # ResNet32 | 5(NA)| 92.46 | 92.49 | NA | NA | 50 ( NA) # ResNet44 | 7(NA)| 92.50 | 92.83 | NA | NA | 70 ( NA) # ResNet56 | 9 (6)| 92.71 | 93.03 | 93.01 | NA | 90 (100) # ResNet110 |18(12)| 92.65 | 93.39+-.16| 93.15 | 93.63 | 165(180) # ResNet164 |27(18)| ----- | 94.07 | ----- | 94.54 | ---(---) # ResNet1001| (111)| ----- | 92.39 | ----- | 95.08+-.14| ---(---) # --------------------------------------------------------------------------- n = 3 # Model version # Orig paper: version = 1 (ResNet v1), Improved ResNet: version = 2 (ResNet v2) version = 1 # Computed depth from supplied model parameter n if version == 1: depth = n * 6 + 2 elif version == 2: depth = n * 9 + 2 # Model name, depth and version model_type = 'ResNet%dv%d' % (depth, version) #sess = K.get_session() dset = 'cifar10' dset = settings['dset'] batch_size = settings['batch'] num_classes = 10 epochs = settings['epochs'] test_acnn = settings['test_layer'] == 'aconv' akernel_size = settings['kernel_size'] data_augmentation = settings['data_augmentation'] num_blocks = settings['depth'] lr_multiplier = settings['lr_multiplier'] nfilters = 16 normalize_data = True acnn_options = { 'init_sigma': 0.15, 'norm': 2, 'kernel_size': (akernel_size, akernel_size) } if dset == 'mnist': acnn_options.update({'dropout': 0.25}) elif dset == 'mnist-clut': normalize_data = False if 'init_sigma' in settings.keys(): acnn_options['init_sigma'] = settings['init_sigma'] if 'norm' in settings.keys(): acnn_options['norm'] = settings['norm'] if 'nfilters' in settings.keys(): nfilters = settings['nfilters'] ld_data = load_dataset(dset, normalize_data, options=[]) x_train, y_train, x_test, y_test, input_shape, num_classes = ld_data inputs = Input(shape=input_shape) outputs = resnet(inputs, num_classes, num_blocks=num_blocks, kernel_size=akernel_size, num_filters=nfilters, acnn=test_acnn, acnn_options=acnn_options) model = Model(inputs, outputs) model.summary() print(model_type) #lr_dict = {'all':0.001,'acnn-1/Sigma:0': 0.001,'acnn-1/Weights:0': 0.001, # 'acnn-2/Sigma:0': 0.001,'acnn-2/Weights:0': 0.001} lr_dict = {'all': 0.01, 'Sigma': 0.01} for i in lr_dict.keys(): lr_dict[i] *= settings['lr_multiplier'] MIN_SIG = 1.0 / akernel_size MAX_SIG = akernel_size * 1.0 mom_dict = {'all': 0.9} clip_dict = {'Sigma': [MIN_SIG, MAX_SIG]} decay_dict = {'all': 0.1} e_i = x_train.shape[0] // batch_size #decay_epochs =np.array([e_i*1,e_i*2,e_i*3,e_i*4,e_i*80,e_i*120,e_i*160], dtype='int64') decay_epochs = np.array([e_i * 80, e_i * 120, e_i * 160], dtype='int64') # WE ACTUALLY USED BELOW FOR THE TESTS OF THE PAPER # WHERE WE GIVE DIFFERENT LEARNING RATE FOR SIGMA.'Sigma':0.1*lr_multiplier} # UNFORTUNATELY THIS DOES NOT WORK IN TF2. # the paper resnet results with sgdwithcycliclr but on tf1.4 # lr_cyclic = True # if lr_cyclic: # opt = SGDwithCyclicLR(peaklriter=epochs/2*e_i,lr=lr_dict, momentum = mom_dict, # min_lr ={'all':0.0001}, #0.0001 # peak_lr={'all':0.5*lr_multiplier,'Sigma':0.1*lr_multiplier}, # lrsigma=0.5, # clips=clip_dict, clipvalue=1.0, verbose=2) # gives 92.4 at 150 epochs for CIFAR from tensorflow_addons.optimizers.cyclical_learning_rate import CyclicalLearningRate # you can use learning schedule below. it increases the learning rate smoothly # until it reaches a max_lr, but for all variables. def gauss_lr(epoch, lr, min_lr=1e-4, max_lr=0.5 * settings['lr_multiplier'], center=epochs / 3, lr_sigma=0.5): print("testing", epoch, lr, min_lr, max_lr, center, lr_sigma) lr = (min_lr + max_lr * np.exp(-(epoch - center)**2 / (center * lr_sigma)**2)) return lr lr_callback = tf.keras.callbacks.LearningRateScheduler(gauss_lr, verbose=1) opt = SGD(momentum=0.9, clipvalue=1.0) # if dset=='lfw_faces': does not get more than 90s # print("USING ADAM") # opt = AdamwithClip(1e-2, clips=clip_dict, clipvalue=1.0) # red_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', # factor=0.9, patience=10, # verbose=1, mode='auto', # min_delta=0.0001, # cooldown=10, min_lr=1e-5) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # Prepare model model saving directory. save_dir = os.path.join(os.getcwd(), 'saved_models') if test_acnn: model_name = dset + '_%s_acnn_resnet_model_best_sofar.h5' % model_type else: model_name = dset + '_%s_resnet_model_best_sofar.h5' % model_type if not os.path.isdir(save_dir): os.makedirs(save_dir) filepath = os.path.join(save_dir, model_name) from tensorflow.keras.utils import plot_model #plot_model(model,'resnet_model.png', show_shapes=True, show_layer_names=False,dpi=300) # Prepare callbacks for model saving and for learning rate adjustment. checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_accuracy', verbose=1, save_best_only=True) # you can try this with adam # lr_reducer = ReduceLROnPlateau(factor=0.9, # cooldown=0, # patience=5, # min_lr=0.5e-6) #callbacks = [checkpoint, lr_reducer, lr_scheduler] stat_func_name = ['max: ', 'mean: ', 'min: ', 'var: ', 'std: '] stat_func_list = [np.max, np.mean, np.min, np.var, np.std] callbacks = [lr_callback] #LearningRateScheduler(pwl) MIN_SIG = 1.0 / akernel_size MAX_SIG = akernel_size * 1.0 if settings['test_layer'] == 'aconv': ccp1 = ClipCallback('Sigma', [MIN_SIG, MAX_SIG]) callbacks += [ccp1] silent_mode = True if not silent_mode: from keras_utils import PrintLayerVariableStats if test_acnn: pr_1 = PrintLayerVariableStats("lv1_blk1_res_conv1_aconv2D", "Weights:0", stat_func_list, stat_func_name) pr_2 = PrintLayerVariableStats("lv1_blk1_res_conv1_aconv2D", "Sigma:0", stat_func_list, stat_func_name) pr_3 = PrintLayerVariableStats("lv1_blk1_res_conv2_aconv2D", "Weights:0", stat_func_list, stat_func_name) pr_4 = PrintLayerVariableStats("lv1_blk1_res_conv2_aconv2D", "Sigma:0", stat_func_list, stat_func_name) callbacks += [pr_1, pr_2, pr_3, pr_4] #,rv_weights_1,rv_sigma_1] else: pass # if dset=='lfw_faces': thisis used with adam. # callbacks+=[red_lr] # Run training, with or without data augmentation. if not data_augmentation: print('Not using data augmentation.') his = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True, callbacks=callbacks) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( # set input mean to 0 over the dataset featurewise_center=False, # set each sample mean to 0 samplewise_center=False, # divide inputs by std of dataset featurewise_std_normalization=False, # divide each input by its std samplewise_std_normalization=False, # apply ZCA whitening zca_whitening=False, # epsilon for ZCA whitening zca_epsilon=1e-06, # randomly rotate images in the range (deg 0 to 180) rotation_range=0, # brightness_range=[0.9,1.1], # randomly shift images horizontally width_shift_range=0.2, # randomly shift images vertically height_shift_range=0.2, # set range for random shear shear_range=0., # set range for random zoom #zoom_range=[0.9,1.1], # set range for random channel shifts channel_shift_range=0., # set mode for filling points outside the input boundaries fill_mode='nearest', # value used for fill_mode = "constant" cval=0., # randomly flip images horizontal_flip=True, # randomly flip images vertical_flip=False, # set rescaling factor (applied before any other transformation) rescale=None, # set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format=None, # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0) # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). his = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), validation_data=(x_test, y_test), epochs=epochs, verbose=1, workers=4, callbacks=callbacks, steps_per_epoch=x_train.shape[0] // batch_size) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) return score, his, model
#훈련 model.compile( loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.01), metrics='acc') es = EarlyStopping(monitor='loss', patience=5, verbose=1, mode="auto",) rl = ReduceLROnPlateau( monitor='val_loss', factor=0.1, patience=2, verbose=1, mode='auto') modelpath = 'C:/data/MC/best_fish_illness_{epoch:02d}-{val_loss:.4f}.hdf5' mc = ModelCheckpoint(filepath = modelpath ,save_best_only=True, mode = 'auto') hist= model.fit(x_train, y_train, epochs = 20, batch_size = 32,validation_data = (x_test, y_test), callbacks=[es, rl, mc]) model.save('C:/data/h5/fish_model2_3.h5') model.save_weights('C:/data/h5/fish_weight_3.h5') # model = load_model('C:/data/h5/fish_model2.h5') # model.load_weights('C:/data/h5/fish_weight.h5') #평가 loss, acc, f1_score, precision, recall = model.evaluate(x_test, y_test) print('loss : ', loss) print('acc : ', acc) print('f1_score : ', f1_score) print('precision : ', precision) print('recall : ', recall) #검증 # predict image filter path = r'C:/data/fish_data/fish_predicts_data/real' # Source Folder dstpath = r'C:/data/fish_data/fish_predicts_data/opencv/image_filter' # Destination Folder try: makedirs(dstpath) except: print ("Directory already exist, images will be written in asme folder")
class cnn_model(base_model): def __init__(self): input_layer = Input(shape=(32, 32, 1), name='input_layer') x = Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(32, 32, 1))(input_layer) x = BatchNormalization()(x) x = Conv2D(32, (3, 3), padding='same', activation='relu')(x) x = MaxPool2D(pool_size=(2, 2))(x) x = Dropout(0.4)(x) x = Conv2D(64, kernel_size=(3, 3), activation='relu')(x) x = BatchNormalization()(x) x = Conv2D(64, kernel_size=(3, 3), activation='relu')(x) x = MaxPool2D(pool_size=(2, 2))(x) x = Dropout(0.4)(x) x = Conv2D(128, kernel_size=(3, 3), activation='relu')(x) x = BatchNormalization()(x) x = Conv2D(128, kernel_size=(3, 3), activation='relu')(x) x = MaxPool2D(pool_size=(2, 2))(x) x = Dropout(0.4)(x) x = Flatten()(x) x = Dense(128, activation='relu')(x) d1 = Dense(11, activation='softmax')(x) d2 = Dense(11, activation='softmax')(x) d3 = Dense(11, activation='softmax')(x) d4 = Dense(11, activation='softmax')(x) d5 = Dense(11, activation='softmax')(x) lst = [d1, d2, d3, d4, d5] self.model = Model(input_layer, lst) def fit_model(self, X_train, y_train, X_val, y_val): y_train = to_categorical(y_train) y_val = to_categorical(y_val) early_stopping = callbacks.EarlyStopping(monitor='val_loss', patience=5) model_checkpoint = callbacks.ModelCheckpoint('multi-digit_cnn_new.h5', save_best_only=True) optimizer = Adam(lr=1e-3, amsgrad=True) tb = callbacks.TensorBoard(log_dir="ccnlogs/{}".format(time())) self.model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) self.history = self.model.fit( X_train, [ y_train[:, 0], y_train[:, 1], y_train[:, 2], y_train[:, 3], y_train[:, 4] ], batch_size=512, epochs=12, shuffle=True, validation_data=(X_val, [ y_val[:, 0], y_val[:, 1], y_val[:, 2], y_val[:, 3], y_val[:, 4] ]), callbacks=[early_stopping, model_checkpoint]) def evaluate(self, X_test, Y_test, mod=False, model_json_file="file.json", weights="weights"): if mod: with open(model_json_file, 'r') as json_file: model_json = json.load(json_file) loaded_model = model_from_json(json.dumps(model_json)) loaded_model.load_weights(weights) optimizer = Adam(lr=1e-3, amsgrad=True) loaded_model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) print( loaded_model.evaluate(X_test, [ Y_test[:, 0], Y_test[:, 1], Y_test[:, 2], Y_test[:, 3], Y_test[:, 4] ], verbose=1)) else: print( self.model.evaluate(X_test, [ Y_test[:, 0], Y_test[:, 1], Y_test[:, 2], Y_test[:, 3], Y_test[:, 4] ], verbose=1))
from tensorflow.keras.models import Model inputs = Input(shape=(8, )) output1 = Dense(64, activation='relu')(inputs) output2 = Dense(32, activation='relu')(output1) output3 = Dense(16, activation='relu')(output2) output4 = Dense(1, activation='sigmoid')(output3) model = Model(inputs, output4) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) history = model.fit(x_train, y_train, epochs=500, batch_size=64, verbose=1) # 딥러닝 시작 scores = model.evaluate(x_test, y_test) print('%s : %.2f%%' % (model.metrics_names[1], scores[1] * 100)) print(x_test[:1]) new_x = [[0.05, 0.84, 0.39, -0.69, 0, -0.10, -0.03, -0.10]] pred = model.predict(new_x) #print('에측결과: ', pred) print("예측결과:", np.where(pred.flatten() > 0.5, 1, 0)) import matplotlib.pyplot as plt plt.plot(history.history['loss']) plt.xlabel('epochs') plt.show()
X_test = X_test.astype('float') / 255.0 # モデルの定義 model = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) # print('Model loaded') # model.summary() top_model = Sequential() top_model.add(Flatten(input_shape=model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(num_classes, activation='softmax')) model = Model(inputs=model.input, outputs=top_model(model.output)) # model.summary() # model.layers:モデルに含れるレイヤーを平滑化したリスト for layer in model.layers[0:15]: layer.trainable = False opt = Adam(lr=0.0001) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) model.fit(X_train, y_train, batch_size=32, epochs=10) score = model.evaluate(X_test, y_test, batch_size=32) model.save('./vgg16_transfer.h5')
def run_training(): """Train the model""" # load data data = pd.read_csv("../data/IMDB Dataset.csv") print('Preprocessing data...') # create binary labels data['label'] = data['sentiment'].map({'negative': 0, 'positive': 1}) Y = data['label'].values # Remove HTML tags data['review'] = [ BeautifulSoup(text, "html.parser").get_text() for text in data['review'] ] # split up the data df_train, df_test, Y_train, Y_test = train_test_split(data['review'], Y, test_size=0.33) # Convert sentences to sequences MAX_VOCAB_SIZE = 10000 tokenizer = Tokenizer(num_words=MAX_VOCAB_SIZE) tokenizer.fit_on_texts(df_train) sequences_train = tokenizer.texts_to_sequences(df_train) sequences_test = tokenizer.texts_to_sequences(df_test) # saving tokenizer print('Saving tokenizer...') with open('../../models/RNN/tokenizer.pickle', 'wb') as handle: pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL) # pad sequences so that we get a N x T matrix data_train = pad_sequences(sequences_train, maxlen=500) data_test = pad_sequences(sequences_test, maxlen=500) ### Create the model print('Building model...') V = len(tokenizer.word_index) # number of unique tokens T = data_train.shape[1] # sequence length D = 32 # Embedding dimensionality M = 10 # Hidden state dimensionality i = Input(shape=(T, )) x = Embedding(V + 1, D)(i) x = LSTM(M, return_sequences=True)(x) x = GlobalMaxPooling1D()(x) x = Dense(1, activation='sigmoid')(x) model = Model(i, x) # Compile and fit model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Training model...') model.fit(data_train, Y_train, epochs=1, validation_split=0.3) # Final evaluation of the model scores = model.evaluate(data_test, Y_test, verbose=0) print("Accuracy: %.2f%%" % (scores[1] * 100)) model.save('../../models/RNN/rnn_model.h5')
dense4 = Dense(35, activation='relu')(dense3) dense5 = Dense(30, activation='relu')(dense4) dense6 = Dense(30, activation='relu')(dense5) dense7 = Dense(25, activation='relu')(dense6) dense8 = Dense(25, activation='relu')(dense7) dense9 = Dense(25, activation='relu')(dense8) outputs = Dense(1)(dense9) model = Model(inputs=input1, outputs=outputs) model.summary() #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mae']) model.fit(x_train, y_train, epochs=100, batch_size=7, validation_split=0.2) #4. 평가, 예측 loss, mae = model.evaluate(x_test, y_test) print("loss : ", loss) print("mae : ", mae) y_predict = model.predict(x_test) #print(y_predict) #RMSE 구하기 from sklearn.metrics import mean_squared_error def RMSE(y_test, y_predict): return np.sqrt(mean_squared_error(y_test, y_predict)) #sqrt는 루트 print("RMSE :", RMSE(y_test, y_predict))
outputs = Dense(1)(aaa) #차례대로 앞에 있던게 맨 뒤로 온다. input을 뒤에 명시 model = Model(inputs, outputs) #model.summary() ''' model = Sequential() model.add(LSTM(10, activation='relu', input_shape=(3,1))) #x변경 됐음 model.add(Dense(20)) model.add(Dense(10)) model.add(Dense(5)) model.add(Dense(1)) #LSTM있는 곳에 디폴트가 탄젠트인것, output은 linear임 # 여기까지는 회귀값이다. 분류값 아님 model.summary() ''' #3. 컴파일 , 훈련 model.compile(loss='mse', optimizer='adam') model.fit(x, y, epochs=10, batch_size=1) #4. 평가, 예측 loss = model.evaluate(x, y) print(loss) x_pred = np.array([50, 60, 70]) #(3,) 행은 하나 -> (1,3,1) x_pred = np.reshape(x_pred, (1, 3, 1)) result = model.predict(x_pred) print(result) ###input shape가 2차원으로 되있을 것 ### 전처리할 때 predict도 transfrom 하기
x = LeakyReLU()(x) x = Dropout(rate=0.5)(x) x = Dense(NUM_CLASSES)(x) output_layer = Activation('softmax')(x) revised_model = Model(input_layer, output_layer) # %% revised_model.summary() # %% # train optimizer = Adam(lr=0.0005) revised_model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy']) # %% revised_model.fit(x_train, y_train, batch_size=32, epochs=10, shuffle=True, validation_data=(x_test, y_test)) # %% len(revised_model.layers) # %% revised_model.layers[5].get_weights() # %% revised_model.layers[6].get_weights() # %% revised_model.evaluate(x_test, y_test)
early_stopping = EarlyStopping( monitor='loss', patience=20, mode='auto' ) # #loss값이 가장낮을 때를 10번 지나갈 때 까지 기다렸다가 stop. mode는 min, max, auto조정가능 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) model.fit(x_train, y_train, epochs=500, batch_size=7, validation_data=(x_val, y_val), verbose=1, callbacks=[early_stopping]) #4. 평가 loss, acc = model.evaluate(x_test, y_test) print('loss : ', loss) print('acc : ', acc) y_pred = model.predict(x_train[-5:-1]) print(y_pred) print(y_train[-5:-1]) #y값 중에서 가장 큰 값을 1로 바꾼다 : argmax #print(np.argmax(y_pred, axis = 0)) print(np.argmax(y_pred, axis=-1)) #print(np.argmax(y_pred, axis = 2)) # loss : 0.14753571152687073 # acc : 0.9666666388511658 # [[2.1295748e-11 1.3989408e-07 9.9999988e-01]
history = model.fit( x=[context_padded_clean_tr, question_padded_clean_tr], y=y_train_tr, # keep 10% of the training data for validation validation_split=0.1, initial_epoch=init_epoch, epochs=num_epochs, callbacks=callbacks, verbose=2, # Logs once per epoch. batch_size=batch_size, # Our neural network will be trained # with stochastic (mini-batch) gradient descent. # It is important that we shuffle our input. shuffle=True, # set to True by default ) # Print training history history = history.history print("\nValidation accuracy: {acc}, loss: {loss}".format( acc=history["val_custom_accuracy"][-1], loss=history["val_loss"][-1])) # Testing print("\nTesting...") model.evaluate( [context_padded_clean_val, question_padded_clean_val], y_train_val, batch_size=batch_size, verbose=1, )
evprofile = evprofile.split('[')[1].split(']')[0].split(', ') for i in range(len(evprofile)): evprofile[i] = float(evprofile[i]) p = policy.Policy() s = state.State(policy, evprofile) for i in range(train_iter): print(i / train_iter) s.print_state() policy = model.predict(np.array([s.return_state()])).tolist() target = get_target(s, discount)[0] print(target) print(policy) model.fit(np.array([s.return_state()]), np.array([target])) loss = model.evaluate(np.array([s.return_state()]), np.array([target])) loss_list.append(loss[0]) p.manual_update(target.index(max(target))) s.update(p) print(p.selection()) #now save model model.save('50000_0.97_5_32LSTM_32_8') with open('50000_0.97_5_32LSTM_32_8.txt', 'w') as f: f.write(str(loss_list)) #test it for i in range(100): pred = model.predict(np.array([s.return_state()])).tolist()[0] option = pred.index(max(pred)) p.manual_update(option)
dense3 = Dense(80)(dense2) dense3 = Dense(40)(dense3) dense3 = Dense(20)(dense3) outputs = Dense(1)(dense3) model = Model(inputs=input1, outputs=outputs) model.summary() # 3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam') from tensorflow.keras.callbacks import EarlyStopping early_stopping = EarlyStopping(monitor='loss', patience=30, mode='auto') model.fit(x, y, epochs=400, batch_size=8, callbacks=[early_stopping]) # 4. 평가, 예측 loss = model.evaluate(x, y, batch_size=8) x_pred = x_pred.reshape(1, 3, 1) y_pred = model.predict(x_pred) print(y_pred) # loss : 0.0086 # y_pred :[[80.141754]] -> batch_size=8, early_stopping적용, relu, 마름모형 # loss: 0.0124 # [[80.06566]] # loss: 0.0088 # [[80.05471]] # 함수형
_ = Dropout(0.25)(_) _ = Flatten()(_) _ = Dense(units=128, activation='relu')(_) _ = Dropout(0.2)(_) _ = Dense(units=num_classes, activation='softmax')(_) model = Model(inputs=inp, outputs=_) model.summary() # 训练 model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(), metrics=['accuracy']) history = model.fit(np.expand_dims(x_train, -1), y_train, batch_size=32, epochs=12, validation_split=0.3) loss, accuracy = model.evaluate(np.expand_dims(x_test, -1), y_test, verbose=0) print(loss, accuracy) # plot the accuracy and loss history fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(14, 5)) ax1.plot(history.history['loss'], label='Train Loss') ax1.plot(history.history['val_loss'], label='Val Loss') ax1.legend() ax2.plot(history.history['accuracy'], label='Train Accuracy') ax2.plot(history.history['val_accuracy'], label='Val Accuracy') ax2.legend()
Input = ResNet() head_model = Input.output head_model = Flatten()(head_model) head_model = Dense(10, activation='softmax')(head_model) model = Model(inputs=Input.input, outputs=head_model) plot_model(model, show_shapes=True, to_file="../architecture_img/archi_resnet_8.png") model.compile(optimizer='adam', loss=keras.losses.sparse_categorical_crossentropy, metrics=['accuracy']) start = time() model.fit(train_x, train_y, epochs=5, validation_data=(val_x, val_y)) training_time = time() - start print(model.evaluate(test_x, test_y)) log_file = open("../architecture_log/archi_resnet_8.log", "w") # save test result log_file.write('test result : ' + str(model.evaluate(test_x, test_y))) test_result_loss = model.evaluate(test_x, test_y)[0] test_result_acc = model.evaluate(test_x, test_y)[1] # save train result log_file.write('train result : ' + str(model.evaluate(test_x, test_y))) train_result_loss = model.evaluate(train_x, train_y)[0] train_result_acc = model.evaluate(train_x, train_y)[1] print('OK: file ../architecture_log/archi_resnet_8.log has been create')
# 모델 선언 model = Model(inputs=[input1, input2], outputs=[output1, output2]) #input 2개 이상은 list 로 묶어줌 model.summary() #======================================================= #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mae']) model.fit([x1_train, x2_train], [y1_train, y2_train], epochs=10, batch_size=1, validation_split=0.2, verbose=2) #4. 평가, 예측 loss = model.evaluate([x1_test, x2_test], [y1_test, y2_test], batch_size=1) print(loss) #[1264.953857421875, 661.8886108398438, 603.0653686523438, 21.079633712768555, 17.948322296142578] print("model.metrics_names : ", model.metrics_names) #model.metrics_names : ['loss', 'dense_13_loss', 'dense_17_loss', 'dense_13_mae', 'dense_17_mae'] y1_predict, y2_predict = model.predict([x1_test, x2_test]) print("=======================") print("y1_predict : \n", y1_predict) print("=======================") print("y2_predict : \n", y2_predict) print("=======================") #RMSE 구하기
class GAN: def __init__(self, d_params, g_params, GAN_params): self.save_dir = GAN_params['save_dir'] self.hidden_layer_output = GAN_params['hidden_layer_output'] self.batch_size = GAN_params['batch_size'] self.discriminator = None self.generator = None self.classifier = None self.model = None self.define_discriminator(d_params) # Compiles a Classifier too self.define_generator(g_params) self.define_gan() self.latent_vectors = self.generator.generate_latent_vectors(100) def define_discriminator(self, params): # Defines Discriminator and Classifier self.discriminator = Discriminator(params) self.classifier = Network(params) self.classifier.model = self.discriminator.c_model self.discriminator.mode, self.classifier.mode = self.discriminator.mode self.discriminator.save_dir, self.classifier.save_dir = self.discriminator.save_dir self.discriminator.link_model_methods() def define_generator(self, params): # Defines generator self.generator = Generator(params) def define_gan(self): # Compiles the GAN model (for training generator weights) self.discriminator.model.trainable = False # Freeze Discriminator weights if not self.hidden_layer_output: out = self.discriminator.model(self.generator.output) else: out = Model(self.discriminator.input, self.discriminator.layers[-1])(self.generator.output) self.model = Model( self.generator.input, out) # Generator input and Discriminator output linked self.model.compile(loss=self.generator.loss, optimizer=self.generator.optimizer) # metrics=self.discriminator.metrics) # Discriminator Values def save(self): # Checkpoints every model of the GAN self.discriminator.save() self.generator.save() self.classifier.save() self.model.save(self.save_dir + 'GAN.h5') def load(self): # Loads every model of the GAN (From save_dir) self.discriminator.load() self.generator.load() self.classifier.load() self.model = load_model(self.save_dir + 'GAN.h5', custom_objects=custom_objects) def check_idle(self, c, m, mean='standard', verbose=False): # Validation loss from (d)iscriminator, (g)enerator, (c)lassifier and (m) best values c, m = np.array(c), np.array(m) if verbose: print(c, m) if mean == 'standard': a = np.mean(c[[0, 2]]) b = np.mean(m[[0, 2]]) elif mean == 'cuadratic': a = np.sqrt( np.mean(np.square(c[[0, 2]])) ) # Squared mean; This penalizes the training if any net gets high losses. b = np.sqrt(np.mean(np.square(m[[0, 2]]))) elif mean == 'accuracy': if 0 in m: return True b = np.mean([c[1], 1 / c[2]]) # Inverse order because higher is better a = np.mean([m[1], 1 / m[2]]) return a < b def get_losses(self, X_real, Y_real_disc, Y_real_class, X_class=None): X_fake, Y_fake_disc, Y_fake_class = self.generator.generate_fake_dataset( X_real.shape[0], Y_real_class.shape[1]) if X_class is None: X_class = X_real X = np.append(X_real, X_fake, axis=0) latent_z = self.generator.generate_latent_vectors(X_real.shape[0]) Y_disc = np.append(Y_real_disc, Y_fake_disc, axis=0) if not self.hidden_layer_output: Y_gen = np.ones(latent_z.shape[0]) else: Y_gen = self.discriminator.last_hidden_model.predict(X_real) c_loss, c_acc = self.classifier.model.evaluate( X_class, Y_real_class, batch_size=self.batch_size, verbose=0) d_loss, d_acc = self.discriminator.model.evaluate( X, Y_disc, batch_size=self.batch_size, verbose=0) g_loss = self.model.evaluate(latent_z, Y_gen, batch_size=self.batch_size, verbose=0) return np.array([d_loss, d_acc, g_loss, c_loss, c_acc]) def train(self, train_params, X_real, Y_real_disc, Y_real_class, X_real_val, Y_real_disc_val, Y_real_class_val): max_epochs = train_params['max_epochs'] max_idle = train_params['max_idle'] batch_size = train_params['batch_size'] verbose = train_params['verbose'] warm_up = train_params['warm_up'] train_ratio = train_params['train_ratio'] fake_ratio = train_params['fake_ratio'] mean = train_params['mean'] max_val_losses = np.array([starting_metric[mean]] * 7) val_loss = np.inf step_count = 0 idle = 0 time_start = time.time() if verbose: print("Started training:") while keep_training(step_count, idle, val_loss, max_epochs, max_idle): with tf.device('/gpu:0'): step_time = time.time() # Generate Dataset for current epoch X_fake, Y_fake_disc, Y_fake_class = self.generator.generate_fake_dataset( X_real.shape[0], Y_real_class.shape[1]) X_fake_val, Y_fake_disc_val, Y_fake_class_val = self.generator.generate_fake_dataset( X_real_val.shape[0], Y_real_class_val.shape[1]) # TRAIN DATASET X_train = np.append(X_real, X_fake, axis=0) latent_z = self.generator.generate_latent_vectors( int(fake_ratio * X_real.shape[0])) Y_train_disc = np.append(Y_real_disc, Y_fake_disc, axis=0) Y_train_class = np.append(Y_real_class, Y_fake_class, axis=0) if not self.hidden_layer_output: Y_train_gen = np.ones(latent_z.shape[0]) else: Y_train_gen = self.discriminator.last_hidden_model.predict( fake_ratio * X_real) # VALIDATION DATASET X_val = np.append(X_real_val, X_fake_val, axis=0) latent_z_val = self.generator.generate_latent_vectors( X_real_val.shape[0]) Y_val_disc = np.append(Y_real_disc_val, Y_fake_disc_val, axis=0) Y_val_class = np.append(Y_real_class_val, Y_fake_class_val, axis=0) if not self.hidden_layer_output: Y_val_gen = np.ones(latent_z_val.shape[0]) else: Y_val_gen = self.discriminator.last_hidden_model.predict( X_real_val) # TRAIN self.discriminator.model.fit(X_train, Y_train_disc, epochs=train_ratio[1], batch_size=batch_size, validation_data=(X_val, Y_val_disc), shuffle=True, verbose=0) self.model.fit(latent_z, Y_train_gen, epochs=train_ratio[0], batch_size=batch_size, validation_data=(latent_z_val, Y_val_gen), shuffle=True, verbose=0) # class_hist = self.classifier.model.fit(X_train, Y_train_class, epochs=train_ratio[1], batch_size=batch_size, validation_data=(X_val, Y_val_class), shuffle=True, verbose=0) val_losses = self.get_losses(X_real_val, Y_real_disc_val, Y_real_class_val) step_count = step_count + 1 if step_count >= warm_up: if self.check_idle(val_losses, max_val_losses, mean): max_val_losses = val_losses idle = 0 self.save() else: idle = idle + 1 step_time = np.round(time.time() - step_time) if verbose: print( 'Step {:3d} idle {:2d}: Disc[{:.5f};{:.5f};{:.5f};{:.5f}]; Gen[{:.5f}]; Time: {:.2f}s' .format(step_count, idle, *val_losses[:-2], step_time)) def train_on_batches(self, train_params, X_real, X_real_class, Y_real_disc, Y_real_class, X_real_val, Y_real_disc_val, Y_real_class_val): max_epochs = train_params['max_epochs'] max_idle = train_params['max_idle'] verbose = train_params['verbose'] plot = train_params['plot'] warm_up = train_params['warm_up'] train_ratio = train_params['train_ratio'] fake_ratio = train_params['fake_ratio'] mean = train_params['mean'] train_classifier = train_params['train_classifier'] step_count = 0 epoch = 0 idle = 0 max_val_losses = [starting_metric[mean]] * 7 step_time = time.time() half_batch = int(self.batch_size / 2) batch_per_epoch = int(X_real.shape[0] / self.batch_size) n_steps = batch_per_epoch * max_epochs print('Epochs: {}; Batch Size: {}; Batch/Epoch: {}; Total Batches: {}'. format(max_epochs, self.batch_size, batch_per_epoch, n_steps)) print('Started training on batches...\n') print( ' D Loss D Acc G Loss C Loss C acc ' ) print( '---------------------------------------------------------------------------------------------' ) while keep_training(epoch, idle, 0, max_epochs, max_idle): step_count += 1 batch_index = np.random.choice(range(X_real.shape[0]), int(half_batch), replace=False) if half_batch > X_real_class.shape[0]: batch_index_class = np.arange(X_real_class.shape[0]) else: batch_index_class = np.random.choice(range( X_real_class.shape[0]), int(half_batch), replace=False) X_fake, Y_fake_disc = self.generator.generate_fake_samples( half_batch), np.zeros(half_batch) latent_z = self.generator.generate_latent_vectors( int(fake_ratio * self.batch_size)) if not self.hidden_layer_output: Y_train_gen = np.ones(latent_z.shape[0]) else: Y_train_gen = self.discriminator.last_hidden_model.predict( X_real[batch_index]) if train_classifier: c_loss, c_acc = self.classifier.model.train_on_batch( X_real_class[batch_index_class], Y_real_class[batch_index_class]) d_loss1, d_acc1 = self.discriminator.model.train_on_batch( X_real[batch_index], Y_real_disc[batch_index]) d_loss2, d_acc2 = self.discriminator.model.train_on_batch( X_fake, Y_fake_disc) g_loss = self.model.train_on_batch(latent_z, Y_train_gen) if step_count % batch_per_epoch == 0: epoch += 1 train_losses = self.get_losses(X_real[batch_index], Y_real_disc[batch_index], Y_real_class[batch_index_class], X_real_class[batch_index_class]) val_losses = self.get_losses(X_real_val, Y_real_disc_val, Y_real_class_val) if epoch > warm_up: if self.check_idle(val_losses, max_val_losses, mean=mean): max_val_losses = val_losses idle = 0 self.save() else: idle += 1 step_time = np.round(time.time() - step_time) if verbose: print( 'Epoch {:3d} idle {:2d}: Disc[{:.5f};{:.5f}]; Gen[{:.5f}]; Class[{:.5f};{:.5f}]; Time: {}s' .format(epoch, idle, *train_losses, step_time)) print( ' Val: Disc[{:.5f};{:.5f}]; Gen[{:.5f}]; Class[{:.5f};{:.5f}]' .format(*val_losses)) if plot: rows = 2 columns = 10 plt.figure(figsize=(15, 3)) for i in range(rows * columns): plt.subplot(rows, columns, i + 1) plt.axis('off') rn = self.generator.generate_fake_samples( 1, latent_z=self.latent_vectors[i].reshape(1, -1)) plt.imshow(rn.reshape(28, 28), cmap='gray') plt.show() step_time = time.time() def score(self, X_real_test, Y_real_class_test, Y_real_disc_test, load_checkpoint=False, show_mode='print'): if load_checkpoint: self.load() X_fake_test, Y_fake_disc_test, Y_fake_class_test = self.generator.generate_fake_dataset( X_real_test.shape[0], Y_real_class_test.shape[1]) X_test = np.append(X_real_test, X_fake_test, axis=0) Y_test_disc = np.append(Y_real_disc_test, Y_fake_disc_test, axis=0) Y_test_class = np.append(Y_real_class_test, Y_fake_class_test, axis=0) self.discriminator.pred(X_test, Y_test_disc, show_mode=show_mode) self.classifier.pred(X_real_test, Y_real_class_test, show_mode=show_mode)
dense1 = Dense(40, activation='relu')(dense1) outputs = Dense(1)(dense1) model = Model(inputs=input1, outputs=outputs) model.summary() #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mae']) from tensorflow.keras.callbacks import EarlyStopping early_stopping = EarlyStopping(monitor='loss', patience=20, mode='auto') model.fit(x_train, y_train, epochs=1000, batch_size=7, validation_data=(x_val, y_val), callbacks=[early_stopping]) #4. 평가, 예측 loss, mae = model.evaluate(x_train, y_train) print("loss : ", loss) print("mae : ", mae) result = np.transpose(y) y_predict = model.predict(result) #Conv1D # loss : 5346.369140625 # mae : 62.078125 #LSTM # loss : 2481.505126953125 # mae : 40.03818130493164
# %% A = GraphConv.preprocess(A).astype("f4") # %% model.compile(optimizer="adam", loss="categorical_crossentropy", weighted_metrics=["acc"]) model.summary() # %% # Prepare data X = X.toarray() A = A.astype("f4") validation_data = ([X, A], y, val_mask) # Train model model.fit( [X, A], y, sample_weight=train_mask, validation_data=validation_data, batch_size=N, shuffle=False, ) # %% # Evaluate model eval_results = model.evaluate([X, A], y, sample_weight=test_mask, batch_size=N) print("Done.\n" "Test loss: {}\n" "Test accuracy: {}".format(*eval_results))
layer1 = Dense(64, activation='relu')(layer1) layer1 = Dense(64, activation='relu')(layer1) output1 = Dense(96)(layer1) model = Model(inputs=input1, outputs=output1) model.summary() model.compile(loss='mse', optimizer='adam') from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau es = EarlyStopping(monitor='val_loss', patience=50, mode='auto') modelpath = "./dacon/data/sunlight_model_binary.hdf5" cp = ModelCheckpoint(filepath=modelpath, monitor='val_loss', save_best_only=True, mode='auto') reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=5, factor=0.5, verbose=1) model.fit(x_train, y_train, epochs=1000, batch_size=256, validation_split=0.2, verbose=2, callbacks=[es, cp, reduce_lr]) loss = model.evaluate(x_test, y_test, batch_size=256) print('loss :', loss)
class VAE(BaseVAE): """ Building and Training a VAE) Modified from: https://github.com/greenelab/tybalt """ def __init__(self, original_dim, intermediate_dim=0, latent_dim=100, epochs=100, batch_size=50, learning_rate=0.01, dropout_rate_input=0, dropout_rate_hidden=0, dropout_decoder=True, freeze_weights=False, classifier_use_z=False, rec_loss="mse", verbose=True): BaseVAE.__init__(self) self.original_dim = original_dim self.intermediate_dim = intermediate_dim self.latent_dim = latent_dim self.epochs = epochs self.batch_size = batch_size self.learning_rate = learning_rate self.dropout_rate_input = dropout_rate_input self.dropout_rate_hidden = dropout_rate_hidden self.dropout_decoder = dropout_decoder self.freeze_weights = freeze_weights self.classifier_use_z = classifier_use_z self.rec_loss = rec_loss self.verbose = verbose self.depth = 2 if (intermediate_dim > 0) else 1 print("AUTOENCODER HAS DEPTH {}".format(self.depth)) def _build_encoder_layers(self): self.inputs = Input(shape=(self.original_dim, ), name="encoder_input") dropout_input = Dropout(rate=self.dropout_rate_input)(self.inputs) if self.depth == 1: z_mean_dense = Dense(self.latent_dim)(dropout_input) z_log_var_dense = Dense(self.latent_dim)(dropout_input) elif self.depth == 2: hidden_dense = Dense(self.intermediate_dim)(dropout_input) hidden_dense_batchnorm = BatchNormalization()(hidden_dense) hidden_dense_encoded = Activation("relu")(hidden_dense_batchnorm) dropout_encoder_hidden = Dropout( rate=self.dropout_rate_hidden)(hidden_dense_encoded) z_mean_dense = Dense(self.latent_dim)(dropout_encoder_hidden) z_log_var_dense = Dense(self.latent_dim)(dropout_encoder_hidden) # Latent representation layers z_mean_dense_batchnorm = BatchNormalization()(z_mean_dense) self.z_mean_encoded = Activation("relu")(z_mean_dense_batchnorm) z_log_var_dense_batchnorm = BatchNormalization()(z_log_var_dense) self.z_log_var_encoded = Activation("relu")(z_log_var_dense_batchnorm) # Sample z self.z = Lambda(self.sampling, output_shape=(self.latent_dim, ), name="z")( [self.z_mean_encoded, self.z_log_var_encoded]) def _build_decoder_layers(self): if self.depth == 1: self.decoder_output = Dense(self.original_dim, activation="sigmoid", name="decoder_output") self.outputs = self.decoder_output(self.z) elif self.depth == 2: self.decoder_hidden = Dense(self.intermediate_dim, activation="relu", name="decoder_hidden") self.decoder_output = Dense(self.original_dim, activation="sigmoid", name="decoder_output") vae_decoder_hidden = self.decoder_hidden(self.z) if self.dropout_decoder == True: self.decoder_dropout = Dropout(rate=self.dropout_rate_hidden) decoder_hidden_dropout = self.decoder_dropout( vae_decoder_hidden) self.outputs = self.decoder_output(decoder_hidden_dropout) else: self.outputs = self.decoder_output(vae_decoder_hidden) def _compile_encoder_decoder(self): # Compile Encoder self.encoder = Model(self.inputs, [self.z_mean_encoded, self.z_log_var_encoded], name="encoder") ''' # Compile Decoder decoder_input = Input(shape=(self.latent_dim,), name='z_sampling') if self.depth==1: x_decoded = self.decoder_output(decoder_input) elif self.depth==2: x_hidden = self.decoder_hidden(decoder_input) if self.dropout_decoder==True: x_dropout = self.decoder_dropout(x_hidden) x_decoded = self.decoder_output(x_dropout) else: X_decoded = self.decoder_output(x_hidden) self.decoder = Model(decoder_input, x_decoded, name='decoder') ''' def _compile_vae(self): """ Compiles all the layers together, creating the Variational Autoencoder """ adam = optimizers.Adam(lr=self.learning_rate) self.vae = Model(self.inputs, self.outputs, name='vae') self.vae.compile(optimizer=adam, loss=self.vae_loss) def build_classifier(self): if (self.freeze_weights): for layer in self.vae.layers: layer.trainable = False if (self.classifier_use_z): self.classifier_output = Dense(5, activation="softmax", name="classifier_output")(self.z) else: self.classifier_output = Dense( 5, activation="softmax", name="classifier_output")(concatenate( [self.z_mean_encoded, self.z_log_var_encoded], axis=1)) self.classifier = Model(self.inputs, self.classifier_output, name="classifier") adam = optimizers.Adam(lr=self.learning_rate) self.classifier.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) def train_vae(self, train_df, val_df, val_flag=True): if (val_flag): self.train_hist = self.vae.fit(train_df, train_df, shuffle=True, epochs=self.epochs, batch_size=self.batch_size, callbacks=[tensorboard], validation_data=(val_df, val_df), verbose=self.verbose) else: self.train_hist = self.vae.fit(train_df, train_df, shuffle=True, epochs=self.epochs, batch_size=self.batch_size, callbacks=[tensorboard], verbose=self.verbose) self.hist_dataframe = pd.DataFrame(self.train_hist.history) def train_stacked_classifier(self, train_df, val_df, epochs): self.classifier.fit(train_df=X_train, y_df=y_labels_train, epochs=epochs, verbose=self.verbose) def evaluate_stacked_classifier(self, X_test, y_test): score = self.classifier.evaluate(X_test, y_test) return score
plot_model(model, to_file="Ch16_3.png", show_shapes=True) # 編譯模型 model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) # 訓練模型 history = model.fit(X_train, Y_train, validation_split=0.2, epochs=10, batch_size=128, verbose=2) # 評估模型 print("\nTesting ...") loss, accuracy = model.evaluate(X_train, Y_train, verbose=0) print("訓練資料集的準確度 = {:.2f}".format(accuracy)) loss, accuracy = model.evaluate(X_test, Y_test) print("測試資料集的準確度 = {:.2f}".format(accuracy)) # 顯示圖表來分析模型的訓練過程 import matplotlib.pyplot as plt # 顯示訓練和驗證損失 loss = history.history["loss"] epochs = range(1, len(loss) + 1) val_loss = history.history["val_loss"] plt.plot(epochs, loss, "bo-", label="Training Loss") plt.plot(epochs, val_loss, "ro--", label="Validation Loss") plt.title("Training and Validation Loss") plt.xlabel("Epochs") plt.ylabel("Loss") plt.legend()
# 载入Mnist手写数据集 mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 x_train = np.expand_dims(x_train,-1) x_test = np.expand_dims(x_test,-1) # 作为输入 inputs = Input([28,28,1]) x = Conv2D(32, kernel_size= 5,padding = 'same',activation="relu")(inputs) x = MaxPooling2D(pool_size = 2, strides = 2, padding = 'same',)(x) x = Conv2D(64, kernel_size= 5,padding = 'same',activation="relu")(x) x = MaxPooling2D(pool_size = 2, strides = 2, padding = 'same',)(x) x = Flatten()(x) x = Dense(1024)(x) x = Dense(256)(x) out = Dense(10, activation='softmax')(x) # 建立模型 model = Model(inputs,out) # 设定优化器,loss,计算准确率 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # 利用fit进行训练 model.fit(x_train, y_train, epochs=5) model.evaluate(x_test, y_test, verbose=2)
activation='relu')(drop_1) conv_4 = Convolution2D(conv_depth_2, kernel_size, strides=kernel_size, padding='same', activation='relu')(conv_3) pool_2 = MaxPooling2D((pool_size, pool_size), padding='same')(conv_4) drop_2 = Dropout(drop_prob_1)(pool_2) flat = Flatten()(drop_2) hidden = Dense(hidden_size, activation='relu')(flat) drop_3 = Dropout(drop_prob_2)(hidden) out = Dense(num_classes, activation='softmax')(drop_3) model = Model(inp, out) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X_train, Y_train, batch_size=batch_size, epochs=num_epochs, verbose=1, validation_split=0.1) model.evaluate(X_test, Y_test, verbose=1) print('model will be saved') model.save('test_img_model') print('~~model saved~~')
aaa = Dense(5, activation='relu')(aaa) outputs = Dense(1)(aaa) model = Model(inputs=inputs1, outputs=outputs) model.summary() #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mae']) model.fit(x_train, y_train, epochs=200, batch_size=10, validation_split=0.2, verbose=1) #4. 평가예측 loss, mae = model.evaluate(x_test, y_test, batch_size=10) print('loss, mae : ', loss, mae) y_predict = model.predict(x_test) #RMSE from sklearn.metrics import mean_squared_error def RMSE(y_test, y_predict): return np.sqrt(mean_squared_error(y_test, y_predict)) print("RMSE : ", RMSE(y_test, y_predict)) #R2 from sklearn.metrics import r2_score
data = pd.read_csv(filename) labels = data['spam'].to_numpy() numeric_data = pd.DataFrame([dict(Counter(t)) for t in data.text]).fillna(0).to_numpy() x_train, x_test, y_train, y_test = train_test_split(numeric_data, labels) #Keras lin = kl.Input((x_train.shape[1],)) out = kl.Dense(1, activation='sigmoid')(lin) model = Model(lin, out) model.compile('adam','binary_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=100) print(model.evaluate(x_test, y_test)) #Sklearn lr = LogisticRegression() lr.fit(numeric_data, data['spam']) print(np.mean(lr.predict(x_test)==y_test)) from sklearn.svm import SVC svm = SVC(probability=True) svm.fit(x_train, y_train) pred = svm.predict_proba(x_test)[:,1] x=[] y=[] for treshold in np.arange(0, 1, 0.01): positives = (pred>treshold)
model = Model(inputs=[input1, input2], outputs=[output1, output2_4]) model.summary() #concatenate는 연산하지 않는다 #3. 컴파일, 훈련 model.compile(loss='mse', optimizer='adam', metrics=['mse']) model.fit([x1_train, x2_train], [y1_train, y2_train], epochs=100, batch_size=8, validation_split=0.25, verbose=1) #4. 평가 result = model.evaluate([x1_test, x2_test], [y1_test, y2_test], batch_size=8) #수치가 5개인 이유: 전체 loss, 각각 두 아웃풋들의 loss 및 mse #전체 loss = 각 아웃풋의 loss+mse 합 #RMSE, R2 y_pred_1, y_pred_2 = model.predict([x1_test, x2_test]) from sklearn.metrics import mean_squared_error from sklearn.metrics import mean_squared_error def RMSE(y_test, y_predict): return np.sqrt(mean_squared_error(y_test, y_predict)) # predict해서 나온 값과 원래 y_test 값을 비교해서 RMSE로 나오게 하겠다
from tensorflow.keras.callbacks import EarlyStopping early_stopping = EarlyStopping( monitor='loss', # loss를 모니터 링하겠다. patience=12, # 내가 원하는 loss값을 20번 확인하겠다. mode='min') # loss를 보면 min으로 준다. 'auto'는 자동 # 최대값을 찍고 그 max나 min이 20번 이상 개선이 없을시. model.fit( [x1, x2], [y1, y2], epochs=500, batch_size=2, verbose=0, callbacks=[early_stopping] #위에 정의한 early_stopping을 fit에다가 정의한다. ) # verbose 훈련의 과정을 출력함. 0 1개만 1 전부다 2 정보 추림 # 4. 평가 예측 loss = model.evaluate([x1, x2], [y1, y2], batch_size=2) print(loss) # input data 도 reshape 으로 변환해줘야됨. x1_input = array([[6.5, 7.5, 8.5], [50, 60, 70], [70, 80, 90], [100, 110, 120]]) # (3,) -> (1, 3) -> (1, 3, 1) x2_input = array([[6.5, 7.5, 8.5], [50, 60, 70], [70, 80, 90], [100, 110, 120]]) # (3,) -> (1, 3) -> (1, 3, 1) x1_input = x1_input.reshape(4, 3, 1) # LSTM 을 사용하기위해 1행, 3열, 1을 자름 로 구성했음. x2_input = x2_input.reshape(4, 3, 1) # LSTM 을 사용하기위해 1행, 3열, 1을 자름 로 구성했음. y_predict = model.predict([x1_input, x2_input]) print("y_predict : ", y_predict)
out = l.add([plus, out]) out = l.AveragePooling2D(pool_size=(K.int_shape(out)[1:3]))(out) out = l.Flatten()(out) #out = l.Dense(512, activation="relu")(out) #ezzel 79.36 out = l.Dense(10, activation='softmax')(out) # ... AveragePooling2D, Flatten, Dense w/ softmax model = Model(inputs=x, outputs=out) if os.path.exists(PATH): model.load_weights(PATH) model.summary() model.compile( optimizer=Adam(lr=LEARNING_RATE, decay=DECAY), #, decay=DECAY loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(train_dataset, batch_size=BATCH_SIZE, epochs=NB_EPOCHS, steps_per_epoch=TRAIN_STEPS, verbose=1, validation_data=val_dataset, validation_steps=VAL_STEPS, callbacks=[checkpoint, reduceLR, earlyStop]) score = model.evaluate(test_dataset, verbose=1, steps=TEST_STEPS) print('Test loss:', score[0]) print('Test accuracy:', score[1])
X_1 = GCNConv(32, activation="relu")([X_in, A_in]) X_1, A_1 = MinCutPool(N // 2)([X_1, A_in]) X_2 = GCNConv(32, activation="relu")([X_1, A_1]) X_3 = GlobalSumPool()(X_2) output = Dense(n_out)(X_3) # Build model model = Model(inputs=[X_in, A_in], outputs=output) opt = Adam(lr=learning_rate) model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["acc"]) model.summary() ################################################################################ # FIT MODEL ################################################################################ model.fit( loader_tr.load(), steps_per_epoch=loader_tr.steps_per_epoch, epochs=epochs, validation_data=loader_va, validation_steps=loader_va.steps_per_epoch, callbacks=[EarlyStopping(patience=10, restore_best_weights=True)], ) ################################################################################ # EVALUATE MODEL ################################################################################ print("Testing model") loss, acc = model.evaluate(loader_te.load(), steps=loader_te.steps_per_epoch) print("Done. Test loss: {}. Test acc: {}".format(loss, acc))