X_train = X_train.reshape(X_train.shape[0], 1, shapex, shapey) X_test = X_test.reshape(X_test.shape[0], 1, shapex, shapey) X_train = X_train.astype("float32") X_test = X_test.astype("float32") X_train /= 255 X_test /= 255 print('X_train shape:', X_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # convert class vectors to binary class matrices Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = NN_Model(n_epochs=nb_epoch, n_batch=batch_size, val_Freq=1) model.add(Convolution2D(nb_filters, 1, nb_conv, nb_conv, border_mode='full')) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(poolsize=(nb_pool, nb_pool))) model.add(Drop_out(0.25)) model.add(Flatten()) # the resulting image after conv and pooling is the original shape # divided by the pooling with a number of filters for each "pixel" # (the number of filters is determined by the last Conv2D) model.add(FC_layer(nb_filters * (shapex / nb_pool) * (shapey / nb_pool), 128)) model.add(Activation('relu')) model.add(Drop_out(0.5))
X_train = X_train.reshape(X_train.shape[0], 1, shapex, shapey) X_test = X_test.reshape(X_test.shape[0], 1, shapex, shapey) X_train = X_train.astype("float32") X_test = X_test.astype("float32") X_train /= 255 X_test /= 255 print('X_train shape:', X_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # convert class vectors to binary class matrices Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = NN_Model(n_epochs=nb_epoch,n_batch=batch_size,val_Freq=1) model.add(Convolution2D(nb_filters, 1, nb_conv, nb_conv, border_mode='full')) model.add(Activation('relu')) model.add(Convolution2D(nb_filters, nb_filters, nb_conv, nb_conv)) model.add(Activation('relu')) model.add(MaxPooling2D(poolsize=(nb_pool, nb_pool))) model.add(Drop_out(0.25)) model.add(Flatten()) # the resulting image after conv and pooling is the original shape # divided by the pooling with a number of filters for each "pixel" # (the number of filters is determined by the last Conv2D) model.add(FC_layer(nb_filters * (shapex / nb_pool) * (shapey / nb_pool), 128)) model.add(Activation('relu')) model.add(Drop_out(0.5))
train=(train_x,train_y) ''' n_y = np.max((np.max(train[1]),np.max(valid[1]))) + 1 print 'number of classes: %i'%n_y print 'number of training data: %i'%len(train[0]) print 'number of validation data: %i'%len(valid[0]) ####build model print 'Initializing model...' mode='tr' model = NN_Model(n_epochs=n_epochs,n_batch=n_batch,snapshot=snapshot_Freq, sample_Freq=sample_Freq,val_Freq=val_Freq,L1_reg=L1_reg,L2_reg=L2_reg) model.add(Embedding(n_words,dim_word)) model.add(Drop_out(0.25)) model.add(GRU(n_u,n_h)) model.add(Drop_out()) model.add(Pool('mean')) model.add(Drop_out()) model.add(FC_layer(n_h,n_y)) model.add(Activation('softmax')) model.compile(optimizer=optimizer,loss=loss) filepath='save/review3.pkl' if mode=='tr':