# for reproducibility np.random.seed(2016) K.set_image_dim_ordering('tf') # define some run parameters batch_size = 32 nb_epochs = 100 examplesPer = 60000 maxToAdd = 8 hidden_units = 200 size = 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 processing X_train_raw = X_train_raw.astype('float32') X_test_raw = X_test_raw.astype('float32') X_train_raw /= 255 X_test_raw /= 255 print('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 setup model = 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.2 model.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 setup model = 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)