def AFNN(filters=8, image_channels=1, use_bnorm=True): layer_count = 0 inpt = Input(shape=(128, 128, image_channels), name='input'+str(layer_count)) # 1st layer, Conv+relu layer_count += 1 x_0 = Conv2D(filters=filters, kernel_size=(3, 3), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', name='conv'+str(layer_count))(inpt) layer_count += 1 x_0 = Activation('relu', name='relu'+str(layer_count))(x_0) # Path 1 layer_count += 1 x1 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p1'+str(layer_count))(x_0) # 2 layers, Conv+BN+relu+MaxPooling for i in range(2): layer_count += 1 x1 = Conv2D(filters=filters, kernel_size=(3, 3), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', use_bias=False, name='conv_p1'+str(layer_count))(x1) if use_bnorm: layer_count += 1 x1 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p1'+str(layer_count))(x1) layer_count += 1 x1 = Activation('relu', name='relu_p1'+str(layer_count))(x1) x1 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p1'+str(layer_count))(x1) i += 1 # Path 2 # 2 layers, Conv+BN+ReLU+MaxPooling layer_count += 1 x_1 = Conv2D(filters=filters, kernel_size=(7, 7), strides=(4, 4), kernel_initializer='Orthogonal', padding='same', name='conv_p2'+str(layer_count))(x_0) layer_count += 1 x_1 = Activation('relu', name='relu_p2'+str(layer_count))(x_1) # Path 2_1 # 1 layer, Conv+BN+ReLU+MaxPooling layer_count += 1 x2 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p2'+str(layer_count))(x_1) layer_count += 1 x2 = Conv2D(filters=filters, kernel_size=(5, 5), strides=(2, 2), kernel_initializer='Orthogonal', padding='same', use_bias=False, name='conv_p2'+str(layer_count))(x2) if use_bnorm: layer_count += 1 x2 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p2'+str(layer_count))(x2) layer_count += 1 x2 = Activation('relu', name='relu_p2'+str(layer_count))(x2) x2 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p2'+str(layer_count))(x2) # Path 2_2 # 1 layer, Conv+BN+ReLU+MaxPooling layer_count += 1 x_2 = Conv2D(filters=filters, kernel_size=(7, 7), strides=(4, 4), kernel_initializer='Orthogonal', padding='same', name='conv_p3'+str(layer_count))(x_1) if use_bnorm: layer_count += 1 x3 = BatchNormalization( axis=3, momentum=0.0, epsilon=0.0001, name='bn_p3'+str(layer_count))(x_2) layer_count += 1 x3 = Activation('relu', name='relu_p3'+str(layer_count))(x3) layer_count += 1 x3 = MaxPooling2D(pool_size=(2, 2), strides=None, padding='same', data_format=None, name='maxpool_p3'+str(layer_count))(x3) # Merge layer layer_count += 1 x = Concatenate(axis=-1, name='concat'+str(layer_count))([x1, x2, x3]) layer_count += 1 x = Flatten(data_format=None, name='Flat'+str(layer_count))(x) # Dense output layer layer_count += 1 x = Dense(128, activation='relu', name='dense'+str(layer_count))(x) layer_count += 1 x = Dropout(0.5, name='dropout'+str(layer_count))(x) layer_count += 1 y = Dense(1, activation='relu', name='dense'+str(layer_count))(x) model = Model(inputs=inpt, outputs=y) return model
# 4 CRP model.add( Convolution2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=(150, 150, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # Flatten Layer model.add(Flatten()) # 1 Hidden Layer\n", model.add(Dense(units=1024, input_shape=(7, ), activation='relu')) # 2 Hidden Layer\n", model.add(Dense(units=720, input_shape=(7, ), activation='relu')) # 3 Hidden Layer\n", model.add(Dense(units=600, input_shape=(7, ), activation='relu')) # 4 Hidden Layer\n", model.add(Dense(units=712, input_shape=(7, ), activation='relu')) # 5 Hidden Layer\n",
def autoencoder_task(dataset, adj, feats, labels, weights=None): adj = sp.hstack([adj, feats]) h, w = adj.shape kwargs = dict( use_bias=True, kernel_initializer='glorot_normal', kernel_regularizer=None, bias_initializer='zeros', bias_regularizer=None, trainable=True, ) data = Input(shape=(w, ), dtype=np.float32, name='data') ### First set of encoding transformation ### encoded = Dense(256, activation='relu', name='encoded1', **kwargs)(data) ### Second set of encoding transformation ### encoded = Dense(128, activation='relu', name='encoded2', **kwargs)(encoded) if dataset == 'pubmed': encoded = Dropout(rate=0.5, name='drop')(encoded) else: encoded = Dropout(rate=0.8, name='drop')(encoded) # the encoder model maps an input to its encoded representation encoder = Model([data], encoded) encoded1 = encoder.get_layer('encoded1') encoded2 = encoder.get_layer('encoded2') ### First set of decoding transformation ### decoded = DenseTied(256, tie_to=encoded2, transpose=True, activation='relu', name='decoded2')(encoded) ### Node classification ### feat_data = Input(shape=(feats.shape[1], )) pred1 = Dense(labels.shape[1], activation='linear')(feat_data) pred2 = Dense(labels.shape[1], activation='linear')(decoded) prediction = add([pred1, pred2], name='prediction') ### Second set of decoding transformation - reconstruction ### decoded = DenseTied(w, tie_to=encoded1, transpose=True, activation='linear', name='decoded1')(decoded) # compile the autoencoder adam = optimizers.Adam(lr=0.001, decay=0.0) autoencoder = Model(inputs=[data, feat_data], outputs=[prediction]) # autoencoder.save("./models/aeModel.h5") autoencoder.compile(optimizer=adam, loss={'prediction': masked_categorical_crossentropy}) if weights is not None: autoencoder.load_weights(weights) return encoder, autoencoder
xgb_preds = np.expm1(model_xgb.predict(X_test)) lasso_preds = np.expm1(model_lasso.predict(X_test)) predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds}) predictions.plot(x = "xgb", y = "lasso", kind = "scatter") preds = 0.7*lasso_preds + 0.3*xgb_preds solution = pd.DataFrame({"id":test.Id, "SalePrice":preds}) #solution.to_csv("lasso_xgb.csv", index = False) from keras.layers import Dense from keras.models import Sequential from keras.regularizers import l1 from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split X_train = StandardScaler().fit_transform(X_train) X_tr, X_val, y_tr, y_val = train_test_split(X_train, y, random_state = 3) print(X_tr.shape) X_tr model = Sequential() #model.add(Dense(256, activation="relu", input_dim = X_train.shape[1])) model.add(Dense(1, input_dim = X_train.shape[1], W_regularizer=l1(0.001))) model.compile(loss = "mse", optimizer = "adam") model.summary() hist = model.fit(X_tr, y_tr, validation_data = (X_val, y_val)) pd.Series(model.predict(X_val)[:,0]).hist()
print(len(train), len(test)) # convert an array of values into a dataset matrix def create_dataset(dataset, look_back=1): dataX, dataY = [], [] for i in range(len(dataset)-look_back-1): a = dataset[i:(i+look_back), 0] dataX.append(a) dataY.append(dataset[i + look_back, 0]) return numpy.array(dataX), numpy.array(dataY) # reshape into X=t and Y=t+1 look_back = 1 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # create and fit Multilayer Perceptron model model = Sequential() model.add(Dense(8, input_dim=look_back, activation= "relu" )) model.add(Dense(1)) model.compile(loss= "mean_squared_error" , optimizer= "adam" ) model.fit(trainX, trainY, nb_epoch=200, batch_size=2, verbose=2) # Estimate model performance trainScore = model.evaluate(trainX, trainY, verbose=0) print( "Train Score: %.2f MSE (%.2f RMSE)" % (trainScore, math.sqrt(trainScore))) testScore = model.evaluate(testX, testY, verbose=0) print("Test Score: %.2f MSE (%.2f RMSE)" % (testScore, math.sqrt(testScore))) # generate predictions for training trainPredict = model.predict(trainX) testPredict = model.predict(testX) # shift train predictions for plotting trainPredictPlot = numpy.empty_like(dataset) trainPredictPlot[:, :] = numpy.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
test_size=0.2, random_state=1234) x_train = x_train.reshape(x_train.shape[0], *(28, 28, 1)) x_test = x_test.reshape(x_test.shape[0], *(28, 28, 1)) x_validate = x_validate.reshape(x_validate.shape[0], *(28, 28, 1)) from keras.models import Sequential from keras.layers import Conv2D, Flatten, Dropout, Dense, MaxPooling2D from keras.optimizers import Adam from keras.callbacks import TensorBoard model = Sequential() model.add(Conv2D(32, 3, 3, input_shape=(28, 28, 1), activation='relu')) model.add(Flatten()) model.add(Dense(output_dim=32, activation='relu')) model.add(Dense(output_dim=10, activation='sigmoid')) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy']) epochs = 50 model.fit(x_train, y_train, batch_size=512, nb_epoch=epochs, verbose=1, validation_data=(x_validate, y_validate)) ''' EVALUATİNG_MODEL
def train_network(x_train, y_train, x_validation, y_validation, groundtruth_validation, learning_rate, optimizer_name): #Information necessary to make the network running batch_size = 32 num_classes = 10 nb_epoch =1 #this is our model (very small network, BTW) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same',input_shape=x_train.shape[1:])) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('sigmoid')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('sigmoid')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) convout1=Activation('sigmoid') model.add(convout1) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) #lets decide which learning algorithm we will use (try all) if (optimizer_name=='rmsprop'): optimizer_params= keras.optimizers.rmsprop(lr=learning_rate, decay=0.0005) if (optimizer_name=='adam'): optimizer_params=keras.optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) if (optimizer_name=='adamax'): optimizer_params=keras.optimizers.Adamax(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) if (optimizer_name=='adadelta'): optimizer_params=keras.optimizers.Adadelta(lr=learning_rate, rho=0.95, epsilon=1e-08, decay=0.0) if (optimizer_name=='adagrad'): optimizer_params=keras.optimizers.Adagrad(lr=learning_rate, epsilon=1e-08, decay=0.0) if (optimizer_name=='sgd'): optimizer_params = keras.optimizers.SGD(lr=learning_rate, decay=0.0005) if (optimizer_name=='nadam'): optimizer_params=keras.optimizers.Nadam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) #ok, lets save this model by compiling it model.compile(loss='categorical_crossentropy', optimizer=optimizer_params, metrics=["accuracy"]) #Lets train and validate using the data we have (no data augmentation) model.fit(x_train, y_train, batch_size=batch_size, epochs=nb_epoch, validation_data=(x_validation, y_validation)) predict_validation=model.predict(x_validation, batch_size=32, verbose=0) target_names = ['HTC-1-M7', 'iPhone-4s', 'iPhone-6', 'LG-Nexus-5x', 'Motorola-Droid-Maxx', 'Motorola-Nexus-6', 'Motorola-X', 'Samsung-Galaxy-Note3', 'Samsung-Galaxy-S4', 'Sony-NEX-7'] predict_validation=np.argmax(predict_validation, axis=1) report=classification_report(groundtruth_validation, predict_validation, target_names=target_names) accuracy=accuracy_score(groundtruth_validation,predict_validation) print(report) text_file = open("accuracies.txt", "a") text_file.write("%f \n" % accuracy) text_file.close() print('Final Accuracy for this experiment: ' + str(accuracy))
train_data,test_data,train_label, test_label = train_test_split(beats,labels,test_size=0.1,stratify=labels,random_state=42) model = '{0} dengan data SVDB non bwr batch 8 - DNN 3 HL 3 class AAMI v5'.format(title) np.savetxt('train data {0}.csv'.format(model),train_data,delimiter=',',fmt='%.3f') np.savetxt('train label {0}.csv'.format(model),train_label,delimiter=',',fmt='%i') np.savetxt('test data {0}.csv'.format(model),test_data,delimiter=',',fmt='%.3f') np.savetxt('test label {0}.csv'.format(model),test_label,delimiter=',',fmt='%i') from keras.layers import Input,Dense from keras.models import Model import random #random.seed(42) import time start_time = time.time() mc = ModelCheckpoint('best_model {0}.h5'.format(model), monitor='val_acc', mode='max', verbose=1, save_best_only=True) inputs = Input(shape=(train_data.shape[1],)) x = Dense(100,activation='relu')(inputs) x = Dense(50,activation='relu')(x) x = Dense(100,activation='relu')(x) #x = Dense(50,activation='relu')(x) #x = Dense(100,activation='relu')(x) #x = Dense(50,activation='relu')(x) #x = Dense(100,activation='relu')(x) outputs = Dense(train_label.shape[1],activation='softmax')(x) dnn = Model(inputs=inputs,outputs=outputs) dnn.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy']) dnn.fit(train_data,train_label,epochs=100,batch_size=32,validation_data=(test_data,test_label),shuffle=False,callbacks=[mc]) lama = time.time() - start_time oa = dnn.history.history['acc'] oa = oa[-1] ova = dnn.history.history['val_acc']
# choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) # the dataset knows the number of features, e.g. 2 n_features = X.shape[2] # separate output y1 = y[:, 0].reshape((y.shape[0], 1)) y2 = y[:, 1].reshape((y.shape[0], 1)) y3 = y[:, 2].reshape((y.shape[0], 1)) # define model visible = Input(shape=(n_steps, n_features)) cnn = Conv1D(64, 2, activation='relu')(visible) cnn = MaxPooling1D()(cnn) cnn = Flatten()(cnn) cnn = Dense(50, activation='relu')(cnn) # define output 1 output1 = Dense(1)(cnn) # define output 2 output2 = Dense(1)(cnn) # define output 3 output3 = Dense(1)(cnn) # tie together model = Model(inputs=visible, outputs=[output1, output2, output3]) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, [y1, y2, y3], epochs=2000, verbose=0) # demonstrate prediction x_input = array([[70, 75, 145], [80, 85, 165], [90, 95, 185]]) x_input = x_input.reshape((1, n_steps, n_features)) yhat = model.predict(x_input, verbose=0)
permute_shape = (MAP_X, MAP_Y, num_zones) elif K.image_dim_ordering() == 'th': # (channels, width, height) model.add(Permute((1, 2, 3), input_shape=input_shape)) permute_shape = (num_zones, MAP_X, MAP_Y) else: raise RuntimeError('Unknown image_dim_ordering.') model.add(Convolution2D(32, (8, 8), strides=(2, 2), padding='same')) model.add(Activation('relu')) model.add(Convolution2D(64, (4, 4), strides=(2, 2), padding='same')) model.add(Activation('relu')) model.add(Convolution2D(64, (3, 3), strides=(1, 1), padding='valid')) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dense(nb_actions)) model.add(Activation('linear')) print(model.summary()) # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and # even the metrics! memory = SequentialMemory(limit=1000000, window_length=1) processor = MicropolisProcessor() class EpsGreedyQPolicyWalker(EpsGreedyQPolicy): def __init__(self): super(EpsGreedyQPolicyWalker, self).__init__()
save_weights_only=True) embeddings_layer = Embedding(len(word_index) + 1, embedding_dim, input_length=max_sequence_length, trainable=True) inputs = Input(shape=(max_sequence_length, ), dtype='int32', name='input') embeddings_sequences = embeddings_layer(inputs) output = LSTM(lstm_output_size, dropout=0.65, recurrent_dropout=0.0)(embeddings_sequences) # output=Dense(64,activation='relu',name='dense1')(output) print(output) output = Dense(1, activation='sigmoid')(output) model = Model(inputs=inputs, outputs=[output]) model.summary() model.compile(loss='binary_crossentropy', optimizer=Adam(0.0003), metrics=['accuracy']) checkpoint_filepath = 'E:/DeepLearning/bully_code/diyu/indrnn.h5' checkpoint = ModelCheckpoint(checkpoint_filepath, monitor='acc', verbose=0, save_best_only=True, mode='max') metrics = Metrics() history = model.fit(x_train, y_train,
# axes[0].set_title('stimulus') # axes[1].imshow(delayed, aspect='auto') # axes[1].set_title('stim delayed lines') # axes[2].imshow(true_strf, aspect='auto') # axes[2].set_title('true STRF') # axes[3].imshow(true_vect[:, None],) # axes[3].set_title('true vector') # uses matrix multiplication to get the STRF response(Y) to the dummy sound (X) Y = np.matmul(true_vect, delayed) # defienes linear model model = Sequential() model.add( Dense(1, input_dim=int(channels * delays), kernel_initializer='normal', activation='linear')) model.compile(loss='mean_squared_error', optimizer='adam') # fits the model and transforms the vector into the equivalent STRF model.fit(delayed.T, Y, batch_size=100, epochs=10000, verbose=1) fit_vect = model.layers[0].get_weights()[0].squeeze() fit_strf = vector_to_strf(fit_vect, delays) # plots the true and fitted STRFS fig, axes = plt.subplots(1, 2) axes = np.ravel(axes) axes[0].imshow(true_strf, aspect='auto') axes[0].set_title('ground truth')
kernel_size=(conv2_size, int(maxpool_1_reshape.shape[2])), padding='valid', kernel_initializer='normal', activation=activation1, name='conv_2')(dropout_2) maxpool_2 = MaxPool2D(pool_size=pool_size, strides=stride_size, padding='valid', name='pool_2')(conv_2) maxpool_2_reshape = Reshape( (int(maxpool_2.shape[1]), int(maxpool_2.shape[3]), 1), name='pool_2_reshaped')(maxpool_2) flatten = Flatten()(maxpool_2_reshape) dropout_3 = Dropout(drop)(flatten) # Dense layers dense_1 = Dense(units=dense_1_neurons, activation=activation2, name='dense_1')(dropout_3) dropout_4 = Dropout(drop)(dense_1) dense_final = Dense(units=dense_final_neurons, activation=activation2, name='dense_final')(dropout_4) dropout_5 = Dropout(drop)(dense_final) softmax_final = Dense(units=len(categories), activation='softmax', name='softmax_final')(dropout_5) # Compile model model = Model(inputs=inputs, outputs=softmax_final) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) # Save checkpoints? if save_checkpoints:
print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
import numpy as np from keras import optimizers from keras.layers import Dense from keras.models import Sequential x_train = [1,2,3,4] y_train = [0,-1,-2,-3] model = Sequential() model.add(Dense(1,input_dim=1)) sgd = optimizers.SGD(lr=0.1) model.compile(optimizer=sgd,loss='mse',metrics = ['accuracy']) model.summary() model.fit(x_train,y_train,epochs = 200) y_predict = model.predict(np.array([5])) print(y_predict)
Y_Train = train_y model = Sequential() model.add(Embedding( input_dim=EMBEDDING_LENGTH, output_dim=EMBEDDING_OUTPUT_DIM, input_length=TWEET_LENGTH, mask_zero=True )) model.add(LSTM( output_dim=50, return_sequences=False )) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('Train...') model.fit(X_Train, Y_Train, batch_size=32, nb_epoch=15, verbose=2) # serialize model to JSON model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json)
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape)) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.summary() bind_model(model) if config.pause: nsml.paused(scope=locals()) bTrainmode = False if config.mode == 'train': bTrainmode = True """ Initiate RMSprop optimizer """
def NIH(max_token_length, vocabulary_size, rnn='gru' ,num_image_features=2048, hidden_size=512, embedding_size=512, regularizer=1e-8): # word embedding text_input = Input(shape=(max_token_length, vocabulary_size), name='text') text_mask = Masking(mask_value=0.0, name='text_mask')(text_input) text_to_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='text_embedding'))(text_mask) text_dropout = Dropout(.5, name='text_dropout')(text_to_embedding) # object image embedding object_image_input = Input(shape=(max_token_length, num_image_features), name='image_object') object_image_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='object_image_embedding'))(object_image_input) object_image_dropout = Dropout(.5,name='object_image_dropout')(object_image_embedding) # scene image embedding scene_image_input = Input(shape=(max_token_length, num_image_features), name='image_scene') scene_image_embedding = TimeDistributed(Dense(units=embedding_size, kernel_regularizer=l2(regularizer), name='scene_image_embedding'))(scene_image_input) scene_image_dropout = Dropout(.5,name='scene_image_dropout')(scene_image_embedding) #object attention object_attention_network = LSTM(units=hidden_size, return_sequences=True, name='object_attention_recurrent_network')(object_image_input) object_attention_network = Activation('tanh')(object_attention_network) object_attention_network = TimeDistributed(Dense(1))(object_attention_network) object_attention_probs = Activation('softmax')(object_attention_network) object_attention_mul =merge([object_image_embedding, object_attention_probs], mode='dot', dot_axes=(1, 1)) object_attention_permute=Permute((2, 1))(object_attention_mul) #scene attention scene_attention_network = LSTM(units=hidden_size, return_sequences=True, name='scene_attention_recurrent_network')(scene_image_input) scene_attention_network = Activation('tanh')(scene_attention_network) scene_attention_network = TimeDistributed(Dense(1))(scene_attention_network) scene_attention_probs = Activation('softmax')(scene_attention_network) scene_attention_mul =merge([object_image_embedding, scene_attention_probs], mode='dot', dot_axes=(1, 1)) scene_attention_permute=Permute((2, 1))(scene_attention_mul) # language model # recurrent_inputs = [text_dropout, image_dropout] left_recurrent_inputs = [text_dropout, object_attention_permute] left_merged_input = Add()(left_recurrent_inputs) right_recurrent_inputs = [text_dropout, scene_attention_permute] right_merged_input = Add()(right_recurrent_inputs) if rnn == 'gru': left_recurrent_network = GRU(units=hidden_size, recurrent_regularizer=l2(regularizer), kernel_regularizer=l2(regularizer), bias_regularizer=l2(regularizer), return_sequences=True, name='forward_recurrent_network')(left_merged_input) right_recurrent_network = GRU(units=hidden_size, recurrent_regularizer=l2(regularizer), kernel_regularizer=l2(regularizer), bias_regularizer=l2(regularizer), return_sequences=True, go_backwards=True, name='backword_recurrent_network')(right_merged_input) # if rnn == 'lstm': # recurrent_network = LSTM(units=hidden_size, # recurrent_regularizer=l2(regularizer), # kernel_regularizer=l2(regularizer), # bias_regularizer=l2(regularizer), # return_sequences=True, # name='recurrent_network')(merged_input) # elif rnn == 'gru': # recurrent_network = GRU(units=hidden_size, # recurrent_regularizer=l2(regularizer), # kernel_regularizer=l2(regularizer), # bias_regularizer=l2(regularizer), # return_sequences=True, # name='recurrent_network')(merged_input) else: raise Exception('Invalid rnn name') merged_recur=merge([left_recurrent_network, right_recurrent_network], mode='sum') output = TimeDistributed(Dense(units=vocabulary_size, kernel_regularizer=l2(regularizer), activation='softmax'), name='output')(merged_recur) inputs = [text_input, object_image_input,scene_image_input] model = Model(inputs=inputs, outputs=output) return model
def dense_net(nb_layers, growth_rate=12, nb_filter=64, bottleneck=True, reduction=0.1, dropout_rate=None, subsample_initial_block=True,classes=2): inputs = Input(shape=(280, 280, 16, 1),name='input') print("0 :inputs shape:", inputs.shape) # 设定每个denseblock中convblock的数量:nb_layers = [3,3,3] concat_axis = -1 # 设定concat的轴(即叠加的轴) bn_axis = -1 # 设定BN的轴(即叠加的轴) nb_dense_block = nb_layers.__len__() # nb_dense_block :denseblock的数量,需要和nb_layers对应,nb_layers = [3,3,3],则nb_dense_block=3,即3个stage,每个stage有3个dense_block final_nb_layer = nb_layers[-1] compression = 1.0 - reduction # denseblock的通道衰减率,即实际输出通道数=原输出通道数x通道衰减率 # Initial convolution ======================================================================================= if subsample_initial_block: initial_kernel = (7, 7, 7) initial_strides = (2, 2, 2) # 这个地方需要跑一下实验看一下222好还是221好 else: initial_kernel = (3, 3, 3) initial_strides = (1, 1, 1) x = Conv3D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same', strides=initial_strides, use_bias=False, name = 'init_conv')(inputs) x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5, name='init_bn')(x) x = Activation('relu')(x) if subsample_initial_block: x = MaxPooling3D((3, 3, 3), strides=(2, 2, 2), padding='same')(x) print("0 :Initial conv shape:", x.shape) # Initial convolution finished ================================================================================ # Add dense blocks start ================================================================================== for block_idx in range(nb_dense_block - 1): #print('db:','db'+str(block_idx)) x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, concat_axis=concat_axis, bn_axis=bn_axis, bottleneck=bottleneck, dropout_rate=dropout_rate, grow_nb_filters=True, dbname = 'db'+str(block_idx)) print(block_idx+1, ":dense_block shape:", x.shape) x = __transition_block(x, nb_filter, compression=compression, concat_axis=concat_axis, bias_allow=False, tbname = 'tb'+str(block_idx)) print(block_idx+1, ":transition_block shape:", x.shape) nb_filter = int(nb_filter * compression) # Add dense blocks finish ================================================================================== # The last dense_block does not have a transition_block x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, concat_axis=concat_axis, bn_axis=bn_axis, bottleneck=bottleneck, dropout_rate=dropout_rate, grow_nb_filters=True, dbname = 'db_last') print(nb_dense_block, ":dense_block shape:", x.shape) x = BatchNormalization(axis=bn_axis, epsilon=1.1e-5, name='bn_last')(x) x = Activation('relu')(x) out = GlobalAveragePooling3D(data_format='channels_last')(x) print("GApooling shape:", out.shape) out_drop = Dropout(rate=0.3)(out) if classes == 1: output = Dense(classes, activation='sigmoid', name='fc1')(out_drop) print("predictions1 shape:", output.shape, 'activition:sigmoid') else: output = Dense(classes, activation='softmax', name='fc1')(out_drop) print("predictions2 shape:", output.shape, 'activition:softmax') #out = Dense(classes, name='fc1')(out_drop) #print("out shape:", out.shape) #output = Activation(activation='sigmoid')(out) model = Model(input=inputs, output=output) #mean_squared_logarithmic_error or binary_crossentropy #model.compile(optimizer=SGD(lr=1e-6, momentum=0.9), loss=EuiLoss, metrics=[y_t, y_pre, Acc] ) return model
# Step 1 - Convolution classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu')) # Step 2 - Pooling classifier.add(MaxPooling2D(pool_size = (2, 2))) # Adding a second convolutional layer classifier.add(Conv2D(32, (3, 3), activation = 'relu')) classifier.add(MaxPooling2D(pool_size = (2, 2))) # Step 3 - Flattening classifier.add(Flatten()) # Step 4 - Full connection classifier.add(Dense(units = 128, activation = 'relu')) classifier.add(Dense(units = 1, activation = 'sigmoid')) # Compiling the CNN classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) # Part 2 - Fitting the CNN to the images from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) test_datagen = ImageDataGenerator(rescale = 1./255)
branch_5 = Sequential() branch_5.add(input_layer) branch_5.add( Conv1D(filters=32, kernel_size=5, padding='same', kernel_regularizer=l2(.01))) branch_5.add(Activation('relu')) branch_5.add(MaxPooling1D(pool_size=2)) branch_5.add(Dropout(0.5)) branch_5.add(BatchNormalization()) branch_5.add(LSTM(100)) model = Sequential() model.add(Merge([branch_3, branch_4, branch_5], mode='concat')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print("") # -+-+-+-+-+-+-+- TRAINING MODEL -+-+-+-+-+-+-+- print("RUNNING MODEL") extra_hist = ExtraHistory() start_time = time.time() hist = model.fit(np.vstack((X_train, X_test)), np.hstack((y_train, y_test)), validation_split=0.5, epochs=inputs.num_epochs, batch_size=global_batch_size,
import numpy import pandas from keras.callbacks import ModelCheckpoint from keras.models import Sequential from keras.layers import Dense seed = 7 numpy.random.seed(seed) dataframe = pandas.read_csv("credit_card_transactions.csv", header=0) dataset = dataframe.values X = dataset[:,0:30] Y = dataset[:,30] model = Sequential() model.add(Dense(30, input_dim=30, init='normal', activation='relu')) model.add(Dense(1, init='normal', activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) filepath = "weights.best.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] model.fit(X, Y, validation_split=0.33, nb_epoch=50, batch_size=5, callbacks=callbacks_list, verbose=1)
data = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = data.load_data() class_names = [ 'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot' ] train_images = train_images / 255.0 test_images = test_images / 255.0 model = Sequential([ Flatten(input_shape=(28, 28)), Dense(128, activation="relu"), Dense(10, activation="softmax") ]) model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) model.fit(train_images, train_labels, epochs=5) prediction = model.predict(test_images) for i in range(5): plt.grid(False) plt.imshow(test_images[i], cmap=plt.cm.binary) plt.xlabel("Actual: " + class_names[test_labels[i]])
activation='relu', input_shape=(28, 28, 1))) model.add( Conv2D(filters=32, kernel_size=(5, 5), padding='Same', activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add( Conv2D(filters=64, kernel_size=(3, 3), padding='Same', activation='relu')) model.add( Conv2D(filters=64, kernel_size=(3, 3), padding='Same', activation='relu')) model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(256, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(10, activation="softmax")) model.summary() optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0) model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]) learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5,
y = dataset[:, 4] x = x.reshape(x.shape[0], x.shape[1], 1) from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=66, test_size=0.23, shuffle=False) #2. 모델 구성 from keras.models import load_model model = load_model('./model/save44.h5') model.add(Dense(1, name='mm')) model.summary() #3. 설명한 후 훈련 from keras.callbacks import EarlyStopping early_stopping = EarlyStopping( monitor='loss', patience=100, mode='auto') # 어느정도 사이즈부터 성능향상에 도움을 주는지 아직 머르겠씁니다 model.compile(loss='mse', optimizer='adam', metrics=['acc']) hist = model.fit(x_train, y_train, epochs=10, batch_size=5, validation_split=0.2, callbacks=[early_stopping]) # 걍 이렇게 넣어도 훈련 시키네용...!! ㅎㅎ print(hist.history)
X.dropna(inplace = True) lc = deepcopy(X['target']) del X['target'] y = [x>0 for x in lc] a = len(X) s = int(a*.7) trainX = X[:s] trainy = y[:s] test0X = X[s:] test0y = y[s:] trainX2 = np.asarray(trainX).reshape((trainX.shape[0], 1, trainX.shape[1])) test0X = np.asarray(test0X).reshape((test0X.shape[0], 1, test0X.shape[1])) keras.backend.clear_session() monkey = Sequential() monkey.add(LSTM(LSTMwidth,input_shape = (trainX2.shape[1], trainX2.shape[2]))) monkey.add(Dense(2)) monkey.add(Activation('softmax')) monkey.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) monkey.fit(trainX2,np.asarray(trainy),nb_epoch=30, validation_data=(test0X,np.asarray(test0y))) balances.append(1 - sum(test0y)/len(test0y)) preds = monkey.predict(test0X) predBalances.append(float(sum(x[0]>x[1] for x in preds))/float(len(preds))) rets = [] del monkey for i in thresh: hm = [int(x[1]-x[0]>i) for x in preds] cum = np.zeros(foreward) tc = list(lc[s:]) for j in range(len(tc)): if hm[j]==1: cum[j%foreward]+=tc[j]
def cifar10_func(): st.title("CIFAR-10") st.write( "The dataset is a collection of images that are commonly used to train machine learning and computer vision algorithms. This dataset is used for training a multiclass classification model that can classify or recognize images belonging to 10 different classes." ) from keras.datasets import cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() classes = [ 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] st.write(" ") st.markdown('**Shape**') st.write('\nTraining dataset :', x_train.shape, "\nTesting dataset :", x_test.shape) st.write("**Data** ") rand_14 = np.random.randint(0, x_train.shape[0], 14) sample_digits = x_train[rand_14] sample_labels = y_train[rand_14] num_rows, num_cols = 2, 7 f, ax = plt.subplots(num_rows, num_cols, figsize=(12, 5), gridspec_kw={ 'wspace': 0.03, 'hspace': 0.01 }, squeeze=True) for r in range(num_rows): for c in range(num_cols): image_index = r * 7 + c ax[r, c].axis("off") ax[r, c].imshow(sample_digits[image_index], cmap='gray') ax[r, c].set_title('%s' % classes[int(sample_labels[image_index])]) plt.show() st.pyplot(clear_figure=False) st.write("**Classes** ") s = "" for i in range(len(classes)): if i is not (len(classes) - 1): s += str(classes[i]).title() s += "," s += " " else: s += str(classes[i]) st.write(s) image_height = x_train.shape[1] image_width = x_train.shape[2] num_channels = 3 x_train_min = x_train.min(axis=(1, 2), keepdims=True) x_train_max = x_train.max(axis=(1, 2), keepdims=True) x_train = (x_train - x_train_min) / (x_train_max - x_train_min) x_test_min = x_test.min(axis=(1, 2), keepdims=True) x_test_max = x_test.max(axis=(1, 2), keepdims=True) x_test = (x_test - x_test_min) / (x_test_max - x_test_min) x_train = np.reshape( x_train, (x_train.shape[0], image_height, image_width, num_channels)) x_test = np.reshape( x_test, (x_test.shape[0], image_height, image_width, num_channels)) y_train, y_test = to_categorical(y_train), to_categorical(y_test) st.write("") st.write("**Build Model**") act = st.selectbox("Choose the type of Activation Function ", ('relu', 'sigmoid', 'tanh')) pad = st.selectbox("Choose the Padding ", ('same', 'valid')) dropout = st.checkbox("Dropout") opt = st.selectbox("Choose the type of Optimizer ", ("adam", "sgd", "rmsprop", "adagrad")) val = st.checkbox('Validation Set') epoch = st.slider("Epochs", 0, 250, step=1) b_s = st.slider("Batch Size", 32, 1024, step=32) st.write("") st.write("") if st.button("Train Model"): model = Sequential() model.add( Conv2D(32, kernel_size=3, activation=act, input_shape=(32, 32, 3))) model.add(BatchNormalization()) if dropout: model.add(Dropout(0.2)) model.add( Conv2D(64, kernel_size=3, strides=1, activation=act, padding=pad)) model.add(BatchNormalization()) model.add(MaxPooling2D((2, 2))) model.add( Conv2D(128, kernel_size=3, strides=1, padding=pad, activation=act)) model.add(BatchNormalization()) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(64, kernel_size=3, activation=act)) model.add(BatchNormalization()) model.add(MaxPooling2D((2, 2))) if dropout: model.add(Dropout(0.2)) model.add(Flatten()) model.add(Dense(512, activation="relu")) model.add(Dropout(0.2)) model.add(Dense(len(classes), activation="softmax")) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) with st.spinner( 'Training may take a while, so grab a cup of coffee, or better, go for a run!' ): if val: result = model.fit(x_train, y_train, batch_size=int(b_s), epochs=int(epoch), validation_split=0.2) else: result = model.fit(x_train, y_train, batch_size=int(b_s), epochs=int(epoch)) st.success("Model Trained.") results = model.evaluate(x_test, y_test, batch_size=128) st.write("Loss: ", results[0]) st.write("Accuracy: ", results[1]) model.save("models/cifar10.h5") st.write("**Predictions** (Random Test Samples)") Images = [] pred = "" for i in range(5): r = np.random.randint(0, len(x_test)) Images.append(x_test[r].reshape(x_train.shape[1], x_train.shape[2])) pred += str(classes[model.predict(x_test[r].reshape( -1, x_train.shape[1], x_train.shape[2], 3)).argmax()]) pred += " " st.image(Images, width=100) st.write(pred)
X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255.0 X_test /= 255.0 y_test = to_categorical(y_test, num_classes) y_train = to_categorical(y_train, num_classes) return (X_train, y_train, X_test, y_test) # %% if not sys.argv.__len__() >= 2: (X_train, y_train, X_test, y_test) = preprocess() model = Sequential() model.add(Dense(512, activation='relu', input_shape=(784,))) model.add(Dense(512, activation='relu')) model.add(Dense(10, activation='softmax')) model.compile(optimizer='adam', loss="categorical_crossentropy", metrics=['accuracy']) model.summary() history = model.fit(X_train, y_train, epochs=epochs, validation_data=(X_test, y_test)) plt.plot(history.history['acc'], 'g-') plt.plot(history.history['val_acc'], 'b-') plt.plot(history.history['loss'], 'r-') plt.xlim(0, 4) # %%
# 构建特征 X_train = train_x.values X_test = test_x.values y = train_y imp = Imputer(missing_values='NaN', strategy ='mean', axis = 0) X_train = imp.fit_transform(X_train) sc = StandardScaler() sc.fit(X_train) X_train = sc.transform(X_train) X_test = sc.transform(X_test) model = Sequential() model.add(Dense(256, input_shape=(X_train.shape[1],))) model.add(Activation('tanh')) model.add(Dropout(0.3)) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.3)) model.add(Dense(512)) model.add(Activation('tanh')) model.add(Dropout(0.3)) model.add(Dense(256)) model.add(Activation('linear')) model.add(Dense(1)) # 这里需要和输出的维度一致 model.add(Activation('sigmoid')) # For a multi-class classification problem model.compile(loss='binary_crossentropy',
time_loss_weights[0] = 0 print 'stack_sizes: ', stack_sizes print 'R_stack_sizes: ', R_stack_sizes print 'A_filt_sizes: ', A_filt_sizes print 'Ahat_filt_sizes: ', Ahat_filt_sizes print 'R_filt_sizes: ', R_filt_sizes prednet = PredNet(stack_sizes, R_stack_sizes, A_filt_sizes, Ahat_filt_sizes, R_filt_sizes, output_mode='error', return_sequences=True) inputs = Input(shape=(nt,) + input_shape) print (nt,) + input_shape errors = prednet(inputs) # errors will be (batch_size, nt, nb_layers) errors_by_time = TimeDistributed(Dense(1, weights=[layer_loss_weights, np.zeros(1)], trainable=False), trainable=False)( errors) # calculate weighted error by layer errors_by_time = Flatten()(errors_by_time) # will be (batch_size, nt) final_errors = Dense(1, weights=[time_loss_weights, np.zeros(1)], trainable=False)( errors_by_time) # weight errors by time model = Model(input=inputs, output=final_errors) model.compile(loss='mean_absolute_error', optimizer='adam') train_generator = RadarGenerator(train_file, nt, batch_size=batch_size, shuffle=True) val_generator = RadarGenerator(val_file, nt, batch_size=batch_size, N_seq=N_seq_val) lr_schedule = lambda \ epoch: 0.001 if epoch < 75 else 0.0001 # start with lr of 0.001 and then drop to 0.0001 after 75 epochs callbacks = [LearningRateScheduler(lr_schedule)] if save_model: if not os.path.exists(WEIGHTS_DIR): os.mkdir(WEIGHTS_DIR)