CRNN add digits

来源:互联网 发布:java逆序输出语法 编辑:程序博客网 时间:2024/05/18 18:20
# for reproducibilitynp.random.seed(2016)K.set_image_dim_ordering('tf')# define some run parametersbatch_size = 32nb_epochs = 100examplesPer = 60000maxToAdd = 8hidden_units = 200size = 28# cutoff          = 1000# the data, shuffled and split between train and test sets(X_train_raw, y_train_temp), (X_test_raw, y_test_temp) = mnist.load_data()# ignore "cutoff" section in full run# X_train_raw     = X_train_raw[:cutoff]# X_test_raw      = X_test_raw[:cutoff]# y_train_temp    = y_train_temp[:cutoff]# y_test_temp     = y_test_temp[:cutoff]# basic image processingX_train_raw = X_train_raw.astype('float32')X_test_raw = X_test_raw.astype('float32')X_train_raw /= 255X_test_raw /= 255print('X_train_raw shape:', X_train_raw.shape)print(X_train_raw.shape[0], 'train samples')print(X_test_raw.shape[0], 'test samples')print("Building model")# define our time-distributed setupmodel = Sequential()model.add(TimeDistributed(Conv2D(8, (4, 4), padding='valid'), input_shape=(maxToAdd, size, size, 1)))model.add(Activation('relu'))model.add(TimeDistributed(Conv2D(16, (3, 3), padding='valid')))# model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2),border_mode='valid')))# model.add(Activation('relu'))# model.add(TimeDistributed(Convolution2D(8, 3, 3, border_mode='valid')))model.add(Activation('relu'))# model.add(Reshape((maxToAdd,np.prod(model.output_shape[-3:])))) #this line updated to work with keras 1.0.2model.add(TimeDistributed(Flatten()))model.add(Activation('relu'))model.add(GRU(units=100, return_sequences=True))model.add(GRU(units=50, return_sequences=False))model.add(Dropout(.2))model.add(Dense(1))
'''
# define our time-distributed setupmodel = Sequential()model.add(TimeDistributed(Convolution2D(8, 4, strides=4, padding='valid', activation='relu'),                          input_shape=(SEQ_LENGTH, height, width, depth)))model.add(TimeDistributed(Convolution2D(16, 3, strides=3, padding='valid', activation='relu')))model.add(TimeDistributed(Flatten()))model.add(GRU(50, return_sequences=True, dropout=.3))model.add(TimeDistributed(Dense(10, activation='softmax')))
'''
rmsprop = RMSprop()model.compile(loss='mean_squared_error', optimizer=rmsprop)import osif os.path.exists('xxx.h5'): model = load_model('xxx.h5')# run epochs of sampling data then trainingfor ep in range(0, nb_epochs): X_train = [] y_train = [] X_test = [] y_test = [] X_train = np.zeros((examplesPer, maxToAdd, size, size, 1)) for i in range(0, examplesPer): # initialize a training example of max_num_time_steps,im_size,im_size output = np.zeros((maxToAdd, size, size, 1)) # decide how many MNIST images to put in that tensor numToAdd = int(np.ceil(np.random.rand() * maxToAdd)) # sample that many images indices = np.random.choice(X_train_raw.shape[0], size=numToAdd) example = X_train_raw[indices] # sum up the outputs for new output exampleY = y_train_temp[indices] output[0:numToAdd, :, :, 0] = example X_train[i, :, :, :, :] = output y_train.append(np.sum(exampleY)) y_train = np.array(y_train) if ep == 0: print("X_train shape: ", X_train.shape) print("y_train shape: ", y_train.shape) model.fit(X_train, y_train, batch_size=batch_size, epochs=1, verbose=1)
原创粉丝点击