コード例 #1
0
ファイル: test_sequential_model.py プロジェクト: 5ke/keras
def test_nested_sequential(in_tmpdir):
    (x_train, y_train), (x_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False)

    model.train_on_batch(x_train[:32], y_train[:32])

    loss = model.evaluate(x_test, y_test, verbose=0)

    model.predict(x_test, verbose=0)
    model.predict_classes(x_test, verbose=0)
    model.predict_proba(x_test, verbose=0)

    fname = 'test_nested_sequential_temp.h5'
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(x_test, y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
コード例 #2
0
def train_48calibration_net(X_train, y_train):
    print (X_train.shape,y_train.shape)
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    X_train = X_train.astype('float32')
    X_train /= 255
    model = Sequential()
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
                            border_mode='valid',
                            input_shape=(3, img_rows, img_cols)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool),strides=(2,2)))
    #model.add(BatchNormalization(mode=2))
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
    #model.add(BatchNormalization(mode=2))
    model.add(Flatten())
    model.add(Dense(256))
    model.add(Activation('relu'))
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adadelta',
                  metrics=['accuracy'])
    model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          verbose=1, validation_split=0.2)
    json_string = model.to_json()
    open('../model/48calibration_architecture.json', 'w').write(json_string)
    model.save_weights('../model/48calibration_weights.h5')
コード例 #3
0
def move_grade_training():
	"""Train a model to predict if a given move is 'good' or not"""
	WHOSE_TURN_DIM = 1 #i.e. X or O
	BOARD_DIM = 81 #i.e. 9x9
	POSS_MOVE_DIM = 81 #ie. same as board size
	INPUT_DIM = WHOSE_TURN_DIM + BOARD_DIM + POSS_MOVE_DIM + POSS_MOVE_DIM #turn, board, last_move, new_move

	NB_EPOCH = 5

	#NOTE: X_PIECE always went first in the training data

	model = Sequential()
	model.add(Dense(2 * INPUT_DIM, input_dim=INPUT_DIM))
	model.add(Dense(INPUT_DIM))
	model.add(Dense(BOARD_DIM))
	model.add(Dense(1)) #predicting if the move was a 'winning' move or not
	model.add(Activation('softmax'))
	model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

	for batch in training_batch_generator():
		X = []
		y = []
		for game, winner in batch:
			#skipping ties for now, as we only want to track winning and losing moves
			if winner == constants.NO_PIECE:
				continue

			#all even values of i represent moves by X_PIECE

			#we are starting with an empty board, and hence, all zeros for previous move (i.e. there was no previous move)
			prev_move = np.zeros(81, dtype='int32')
			for i, board in enumerate(game[:-1]):
				#case 1, X won and this is an x move we are scoring
				if i % 2 == 0 and winner == constants.X_PIECE:
					y.append(1)
				#case 2, O won and this is an O move we are scoring
				elif (i % 2 == 1) and winner == constants.O_PIECE:
					y.append(1)
				else:
				#this is a loser's move
					y.append(0)

				turn = i % 2
				x1 = np.asarray(game[i].flatten()) #board
				x2 = prev_move
				row, col = get_move_for_boards(game[i], game[i + 1])
				move_idx = np.ravel_multi_index((row, col), (9, 9))
				x3 = to_categorical([move_idx], BOARD_DIM)[0]
				X.append(np.hstack([[turn], x1, x2, x3]))

				#we need to update the previous move for next iteration
				prev_move = x3


		X = np.asarray(X)
		y = np.asarray(y)
		model.fit(X, y, batch_size=32, nb_epoch=NB_EPOCH, validation_split=0.05)

	with open('keras_model.json', 'w') as f:
		f.write(model.to_json())
コード例 #4
0
def create_model(X_train, Y_train):
    """create_model will create a very simple neural net model and  save the weights in a predefined directory.

    Args:
        X_train:    Input X_train
        Y_train:    Lables Y_train
    """
    xin = X_train.shape[1]

    model = Sequential()
    model.add(Dense(units=4, input_shape=(xin, )))
    model.add(Activation('tanh'))
    model.add(Dense(4))
    model.add(Activation('linear'))
    model.add(Dense(1))

    rms = kop.RMSprop()

    print('compiling now..')
    model.compile(loss='mse', optimizer=rms)

    model.fit(X_train, Y_train, epochs=1000, batch_size=1, verbose=2)
    score = model.evaluate(X_train, Y_train, batch_size=1)
    print("Evaluation results:", score)
    open('pickles/my_model_architecture.json', 'w').write(model.to_json())

    print("Saving weights in: ./pickles/my_model_weights.h5")
    model.save_weights('pickles/my_model_weights.h5')
コード例 #5
0
def test_nested_sequential():
    (X_train, y_train), (X_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_test, y_test, verbose=0)

    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)

    fname = "test_nested_sequential_temp.h5"
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_test, y_test, verbose=0)
    assert loss == nloss

    # test serialization
    config = model.get_config()
    new_model = Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    new_model = model_from_json(json_str)

    yaml_str = model.to_yaml()
    new_model = model_from_yaml(yaml_str)
コード例 #6
0
ファイル: model_construct.py プロジェクト: kkamataki/kaggle
def run_mlp(**args):

    print("building mlp model:")
    print(args["training_data"].shape[0])
    print(args["training_data"].shape[1])
    model = Sequential()
    model.add(Dense(output_dim=512, input_dim=args["training_data"].shape[1], activation="relu"))
    # model.add(Dense(output_dim=64, input_dim=128, activation='relu'))
    # model.add(Dense(output_dim=32, input_dim=64, activation='relu'))
    model.add(Dense(1))
    model.add(Activation("linear"))
    model.compile(loss="mse", optimizer="rmsprop")

    model.fit(args["training_data"], args["training_label"], nb_epoch=50, batch_size=512)

    # pickle.dump(model, open('mlp_testmodel.p', 'w'), protocol=4)
    json_string = model.to_json()
    open("mlp_model_architecture.json", "w").write(json_string)
    model.save_weights("mlp_model_weights.h5", overwrite=True)

    # output = model.evaluate(args['test_data'], args['test_label'], batch_size=512)
    output = model.predict(args["test_data"], verbose=1, batch_size=512)
    output_int = list(map(lambda e: int(e), np.round(output)))
    pickle.dump(output_int, open("mlp_output.p", "wb"), protocol=4)

    return output_int
コード例 #7
0
    def train_model(self):
        print '=======begin to prepare data at ' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '========='
        list_sorted = self.word2vec()

        self.y = np.array(list(self.y))
        self.x = list(sequence.pad_sequences(list(self.x), maxlen=max_len))
        self.x = np.array(list(self.x))
        print '=======end to prepare data at ' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '========='

        print '=======begin to train model at ' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '========='
        model = Sequential()
        model.add(Embedding(input_dim=len(list_sorted) + 1, output_dim=256, input_length=max_len))
        model.add(LSTM(128))
        model.add(Dropout(0.5))
        model.add(Dense(1))
        model.add(Activation('sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer='adam')
        model.fit(self.x, self.y, batch_size=16, nb_epoch=10)

        json_string = model.to_json()
        open('sa_model_architecture.json', 'w').write(json_string)
        model.save_weights('sa_model_weights.h5', overwrite=True)
        print '=======end to train model at ' + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + '========='

        return model
コード例 #8
0
ファイル: train.py プロジェクト: lericson/pokemonnamegen
def main(args=sys.argv[1:]):
    model = Sequential()
    model.add(Embedding(vocsize + 1, sizes[0], mask_zero=True,
                        input_length=seq_length))
    for size in sizes[:-1]:
        model.add(LSTM(128, return_sequences=True))
    model.add(LSTM(sizes[-1]))
    model.add(Dense(vocsize))
    model.add(Activation('softmax'))

    print('x.shape:', x.shape)
    print('y.shape:', y.shape)

    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop')

    with open('topology.json', 'w') as f:
        f.write(model.to_json())

    for iteration in range(1, num_batches//batches_per_it):
        print()
        print('-' * 50)
        print('Iteration', iteration)

        model.fit(x, y, batch_size=batch_size,
                  nb_epoch=batches_per_it, verbose=True)
        model.save_weights('brain-{}.h5'.format(iteration))
コード例 #9
0
ファイル: tutorial.py プロジェクト: sdlirjc/algorithm
def train():
    X, Y = load_data()

    # create model
    model = Sequential()
    # input_dim is the feature number 8 
    #model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
    model.add(Dense(256, input_dim=8, init='uniform', activation='relu'))
    model.add(Dense(16, init='uniform', activation='relu'))
    model.add(Dense(1, init='uniform', activation='sigmoid'))

    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    # Fit the model
    #model.fit(X, Y, nb_epoch=150, batch_size=10)
    model.fit(X, Y, nb_epoch=1000, batch_size=32, shuffle=True)

    # serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")
コード例 #10
0
def test_recursive():
    # test layer-like API
    graph = Graph()
    graph.add_input(name='input1', input_shape=(32,))
    graph.add_node(Dense(16), name='dense1', input='input1')
    graph.add_node(Dense(4), name='dense2', input='input1')
    graph.add_node(Dense(4), name='dense3', input='dense1')
    graph.add_output(name='output1', inputs=['dense2', 'dense3'],
                     merge_mode='sum')

    seq = Sequential()
    seq.add(Dense(32, input_shape=(32,)))
    seq.add(graph)
    seq.add(Dense(4))

    seq.compile('rmsprop', 'mse')

    seq.fit(X_train_graph, y_train_graph, batch_size=10, nb_epoch=10)
    loss = seq.evaluate(X_test_graph, y_test_graph)

    # test serialization
    config = seq.get_config()
    new_graph = Sequential.from_config(config)

    seq.summary()
    json_str = seq.to_json()
    new_graph = model_from_json(json_str)

    yaml_str = seq.to_yaml()
    new_graph = model_from_yaml(yaml_str)
コード例 #11
0
ファイル: train.py プロジェクト: bpig/pickaxe
def make_model(model_dir, input_dim):
    print input_dim
    model = Sequential()
    model.add(Dense(2048, input_dim=input_dim, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(2048, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(2048, activation="relu"))
    model.add(Dropout(0.3))
    model.add(Dense(1))

    # model.add(LSTM(512, return_sequences=True, 
    #                input_shape=(None, input_dim)))
    # model.add(LSTM(1))
    # model.add(Dense(1))

    # model.add(TimeDistributedDense(1024, input_dim=1530))
    # model.add(LSTM(1024, activation='relu', return_sequences=False))
    # model.add(Dropout(0.3))


    model.summary()
    model_path = model_dir + "/model.json"
    with open(model_path, 'w') as f:
        f.write(model.to_json())
    return model
コード例 #12
0
def run_mlp(**args):

  print("building mlp model:")
  print(args['training_data'].shape[0])
  print(args['training_data'].shape[1])
  model = Sequential()
  model.add(Dense(output_dim=512, input_dim=args['training_data'].shape[1], activation='relu'))
  model.add(Dense(1))
  model.add(Activation('linear'))
  model.compile(loss='mse', optimizer='rmsprop')

  model.fit(args['training_data'], args['training_label'], nb_epoch=20, batch_size=512)

  json_string = model.to_json()
  open('mlp_model_architecture.json', 'w').write(json_string)
  model.save_weights(args['output_weight_filename'], overwrite=True)

  output = model.predict(args['test_data'], verbose=1, batch_size=512)

  if (args['output_type']=='int'):
    output_int = list(map(lambda e:int(e), np.round(output)))
    pickle.dump(output_int, open(args['output_feat_filename'], 'wb'), protocol=4)
    return output_int
  else:
    pickle.dump(output, open(args['output_feat_filename'], 'wb'), protocol=4)
    return output
コード例 #13
0
ファイル: TrainMatt.py プロジェクト: HaliteChallenge/Halite
def trainModel():
    inputs, correctOutputs = getNNData()

    print("Collected data")

    trainingInputs = inputs[:len(inputs)//2]
    trainingOutputs = correctOutputs[:len(correctOutputs)//2]

    testInputs = inputs[len(inputs)//2:]
    testOutputs = correctOutputs[len(correctOutputs)//2:]

    model = Sequential()
    model.add(Dense(24, input_shape=(24, )))
    model.add(Activation('tanh'))
    model.add(Dense(24))
    model.add(Activation('tanh'))
    model.add(Dense(5))
    model.add(Activation('softmax'))

    model.summary()

    model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True))

    model.fit(trainingInputs, trainingOutputs, validation_data=(testInputs, testOutputs))
    score = model.evaluate(testInputs, testOutputs, verbose=0)
    print(score)

    json_string = model.to_json()
    open('my_model_architecture.json', 'w').write(json_string)
    model.save_weights('my_model_weights.h5', overwrite=True)
コード例 #14
0
ファイル: dnddqn.py プロジェクト: jsupratman13/workspace
 def create_neural_network(self):
     model = Sequential()
     model.add(Dense(100, input_dim=self.nstates, activation='relu'))
     model.add(Dense(100, activation='relu'))
     model.add(Dense(self.nactions,activation='linear'))
     
     #get second last layer of the model, abondon the last layer
     layer = model.layers[-2]
     nb_action = model.output._keras_shape[-1]
    
     #layer y has a shape(nb_action+1)
     #y[:,0] represents V(s;theta)
     #y[:,1] represents A(a;theta)
     y = Dense(nb_action+1, activation='linear')(layer.output)
    
     #calculate the Q(s,a,;theta)
     #dueling type average -> Q(s,a;theta) = V(s;theta) + (A(s,a;theta)-Average_a(A(s,a;theta)))
     #outputlayer = Lambda(lambda a:K.expand_dims(a[:,0], -1) + a[:,1:] - K.mean(a[:,1:], keepdims=True), output_shape=(nb_action,))(y)
     #dueling type max     -> Q(s,a;theta) = V(s;theta) + (A(s,a;theta)-Max_a(A(s,a;theta)))
     outputlayer = Lambda(lambda a:K.expand_dims(a[:,0], -1) + a[:,1:] - K.max(a[:,1:,], keepdims=True), output_shape=(nb_action,))(y)
     #dueling type naive   -> Q(s,a;theta) = V(s;theta) + A(s,a;theta)
     #outputlayer = Lambda(lambda a: K.expand_dims(a[:,0], -1) + a[:,1:], output_shape=(nb_action,))(y)
    
     #connect
     model = Model(input=model.input, output=outputlayer)
    
     model.compile(loss='mse', optimizer=Adam(lr=self.alpha))
     model_json = model.to_json()
     with open('cartpole.json','w') as json_file:
         json_file.write(model_json)
     return model
コード例 #15
0
ファイル: main.py プロジェクト: ducminhkhoi/Image-Captioning
def load_model(model_name, weights_path=None, model_caption=None):
    print("Loading model")

    if not model_caption:
        model_caption = image_caption_model

    image_caption_json = 'models/'+dataname+'/image_captioning_' + model_name + 'model_'+str(model_caption)+\
                         '_output_rnn_'+str(output_rnn_dim)+'.json'

    if weights_path:  # Second tim run or predict mode
        model = model_from_json(open(image_caption_json).read())
        model.load_weights(weights_path)
    else:

        if image_caption_model == 1:
            image_model = Sequential()
            image_model.add(Dense(word_vec_dim, input_dim=img_dim))
            image_model.add(Reshape((1,word_vec_dim)))

            language_model = Sequential()
            language_model.add(Embedding(vocab_size, word_vec_dim, input_length=max_caption_len))
            language_model.add(GRU(output_dim=word_vec_dim, return_sequences=True))
            language_model.add(TimeDistributedDense(word_vec_dim))

            model = Sequential()
            model.add(Merge([image_model, language_model], mode='concat', concat_axis=1))
            model.add(getattr(layers, model_name)(output_rnn_dim, return_sequences=False, input_shape=(max_caption_len+1, word_vec_dim),stateful=False))
            model.add(Dense(vocab_size, activation='softmax'))

        elif image_caption_model == 2:

            image_model1 = Sequential()
            image_model1.add(Dense(150, input_dim=img_dim))
            image_model1.add(RepeatVector(max_caption_len))

            image_model2 = Sequential()
            image_model2.add(Dense(450, input_dim=img_dim))
            image_model2.add(Reshape((1,450)))

            language_model = Sequential()
            language_model.add(Embedding(vocab_size, 300, input_length=max_caption_len))
            language_model.add(GRU(output_dim=300, return_sequences=True))
            language_model.add(TimeDistributedDense(300))

            model1 = Sequential()
            model1.add(Merge([image_model1, language_model],mode='concat', concat_axis=-1))

            model = Sequential()
            model.add(Merge([image_model2, model1], mode='concat', concat_axis=1))
            model.add(getattr(layers, model_name)(output_rnn_dim, input_shape=(max_caption_len+1, 450),
                                                  return_sequences=False, stateful=False))
            model.add(Dense(vocab_size, activation='softmax'))

        json_string = model.to_json()
        open(image_caption_json, 'w').write(json_string)

    opt = Adagrad()
    model.compile(loss='categorical_crossentropy', optimizer=opt)
    return model
コード例 #16
0
ファイル: test_recurrent.py プロジェクト: eco2116/keras
def test_batch_input_shape_serialization():
    model = Sequential()
    model.add(embeddings.Embedding(2, 2,
                                   mask_zero=True,
                                   input_length=2,
                                   batch_input_shape=(2, 2)))
    json_data = model.to_json()
    reconstructed_model = model_from_json(json_data)
    assert(reconstructed_model.input_shape == (2, 2))
コード例 #17
0
def test_merge_sum():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)

    # test weight saving
    fname = 'test_merge_sum_temp.h5'
    model.save_weights(fname, overwrite=True)
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))
    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))
    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.load_weights(fname)
    os.remove(fname)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
コード例 #18
0
ファイル: mnist_cnn.py プロジェクト: mstallone/Reras
class MNISTClassifier(object):

    def __init__(self, batch_size=128, nb_classes=10):
        self.img_rows, self.img_cols = 28, 28
        self.batch_size = batch_size
        self.nb_classes = nb_classes

    def _loadAndPreprocessTraining(self):
        (self.X_train, self.y_train), (self.X_test, self.y_test) = mnist.load_data()

        self.X_train = self.X_train.reshape(self.X_train.shape[0], 1, self.img_rows, self.img_cols).astype('float32') / 255
        self.X_test = self.X_test.reshape(self.X_test.shape[0], 1, self.img_rows, self.img_cols).astype('float32') / 255

        self.Y_train = np_utils.to_categorical(self.y_train, self.nb_classes)
        self.Y_test = np_utils.to_categorical(self.y_test, self.nb_classes)

    def buildNN(self):
        if os.path.isfile("MNISTArchitecture.json"):
            print("  loading architecture")
            self.model = model_from_json(open('MNISTArchitecture.json').read())
        else:
            self.model = Sequential()

            self.model.add(Convolution2D(64, 5, 5, input_shape=(1, self.img_rows, self.img_cols)))
            self.model.add(Activation('relu'))
            self.model.add(MaxPooling2D(pool_size=(2, 2)))

            self.model.add(Flatten())
            self.model.add(Dense(256))
            self.model.add(Activation('relu'))
            self.model.add(Dropout(0.5))
            self.model.add(Dense(self.nb_classes))
            self.model.add(Activation('softmax'))

            mnistArchitecture = self.model.to_json()
            open('MNISTArchitecture.json', 'w').write(mnistArchitecture)
            pass

        self.model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.0000001, momentum=0.8, nesterov=True), metrics=['accuracy'])

        if os.path.isfile("MNISTWeights.h5"):
            print("  loading weights")
            self.model.load_weights('MNISTWeights.h5')

    def train(self, nb_epoch=24):
        self._loadAndPreprocessTraining()
        self.model.fit(self.X_train, self.Y_train, batch_size=self.batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(self.X_test, self.Y_test))
        self.model.save_weights('MNISTWeights.h5', overwrite=True)

        score = self.model.evaluate(self.X_test, self.Y_test, verbose=0)
        print('Test score:', score[0])
        print('Test accuracy:', score[1])

    def predict(self, data):
        data = data.reshape(data.shape[0], 1, 28, 28).astype('float32') / 255
        return self.model.predict_classes(data, batch_size=32)
コード例 #19
0
ファイル: dqn.py プロジェクト: jsupratman13/workspace
 def create_neural_network(self):
     model = Sequential()
     model.add(Dense(100,input_dim=self.nstates, activation='relu'))
     model.add(Dense(100,activation='relu'))
     model.add(Dense(self.nactions,activation='linear'))
     model.compile(loss='mse', optimizer=Adam(lr=self.alpha))
     model_json = model.to_json()
     with open('cartpole.json','w') as json_file:
         json_file.write(model_json)
     return model
コード例 #20
0
ファイル: lstmcws.py プロジェクト: 450586509/DLNLP
def train(cwsInfo, cwsData, modelPath, weightPath):

    (initProb, tranProb), (vocab, indexVocab) = cwsInfo
    (X, y) = cwsData

    train_X, test_X, train_y, test_y = train_test_split(X, y , train_size=0.9, random_state=1)

    #train_X = [ [ [],[],...,[] ] ]  train_y = [ [],[],...,[] ]
    train_X = np.array(train_X)
    train_y = np.array(train_y)
    test_X = np.array(test_X)
    test_y = np.array(test_y)

    outputDims = len(corpus_tags)
    Y_train = np_utils.to_categorical(train_y, outputDims)
    Y_test = np_utils.to_categorical(test_y, outputDims)
    batchSize = 128
    vocabSize = len(vocab) + 1
    wordDims = 100
    maxlen = 7
    hiddenDims = 100

    w2vModel = Word2Vec.load('model/sougou.char.model')
    embeddingDim = w2vModel.vector_size
    embeddingUnknown = [0 for i in range(embeddingDim)]
    embeddingWeights = np.zeros((vocabSize + 1, embeddingDim))
    for word, index in vocab.items():
        if word in w2vModel:
            e = w2vModel[word]
        else:
            e = embeddingUnknown
        embeddingWeights[index, :] = e

    #LSTM
    model = Sequential()
    model.add(Embedding(output_dim = embeddingDim, input_dim = vocabSize + 1,
        input_length = maxlen, mask_zero = True, weights = [embeddingWeights]))
    model.add(LSTM(output_dim = hiddenDims, return_sequences = True))
    model.add(LSTM(output_dim = hiddenDims, return_sequences = False))
    model.add(Dropout(0.5))
    model.add(Dense(outputDims))
    model.add(Activation('softmax'))
    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam')

    result = model.fit(train_X, Y_train, batch_size = batchSize,
                    nb_epoch = 20, validation_data = (test_X,Y_test), show_accuracy=True)

    j = model.to_json()
    fd = open(modelPath, 'w')
    fd.write(j)
    fd.close()

    model.save_weights(weightPath)

    return model
コード例 #21
0
ファイル: wrappers_test.py プロジェクト: dansbecker/keras
def test_Bidirectional():
    rnn = layers.SimpleRNN
    samples = 2
    dim = 2
    timesteps = 2
    output_dim = 2
    dropout_rate = 0.2
    for mode in ['sum', 'concat']:
        x = np.random.random((samples, timesteps, dim))
        target_dim = 2 * output_dim if mode == 'concat' else output_dim
        y = np.random.random((samples, target_dim))

        # test with Sequential model
        model = Sequential()
        model.add(wrappers.Bidirectional(rnn(output_dim, dropout=dropout_rate,
                                             recurrent_dropout=dropout_rate),
                                         merge_mode=mode,
                                         input_shape=(timesteps, dim)))
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, epochs=1, batch_size=1)

        # test config
        model.get_config()
        model = model_from_json(model.to_json())
        model.summary()

        # test stacked bidirectional layers
        model = Sequential()
        model.add(wrappers.Bidirectional(rnn(output_dim,
                                             return_sequences=True),
                                         merge_mode=mode,
                                         input_shape=(timesteps, dim)))
        model.add(wrappers.Bidirectional(rnn(output_dim), merge_mode=mode))
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, epochs=1, batch_size=1)

        # test with functional API
        inputs = Input((timesteps, dim))
        outputs = wrappers.Bidirectional(rnn(output_dim, dropout=dropout_rate,
                                             recurrent_dropout=dropout_rate),
                                         merge_mode=mode)(inputs)
        if dropout_rate and K.backend() != 'cntk':
            # Dropout is disabled with CNTK for now.
            assert outputs._uses_learning_phase
        model = Model(inputs, outputs)
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, epochs=1, batch_size=1)

        # Bidirectional and stateful
        inputs = Input(batch_shape=(1, timesteps, dim))
        outputs = wrappers.Bidirectional(rnn(output_dim, stateful=True),
                                         merge_mode=mode)(inputs)
        model = Model(inputs, outputs)
        model.compile(loss='mse', optimizer='sgd')
        model.fit(x, y, epochs=1, batch_size=1)
コード例 #22
0
ファイル: model.py プロジェクト: etoestja/inf
def initialize_model():

    model = Sequential()
    model.add(Conv2D(16, (4, 4), strides=(2, 2), padding='valid',
                     input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(16, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(32, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(32, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(64, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3), padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(50))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dense(NB_CLASSES))
    model.add(BatchNormalization())
    model.add(Activation('softmax'))

    opt = Adam(lr=LEARNING_RATE, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    model.compile(loss = "categorical_crossentropy", optimizer = opt, metrics=['accuracy'])

    print model.summary()

    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)

    return model
コード例 #23
0
ファイル: sina_cnn.py プロジェクト: duxiu727/12306_captcha
def cnn():
    train_data, train_label, test_data, test_label = load_data()
    train_data = train_data / 255.0
    test_data = test_data / 255.0

    print(train_data.shape[0], 'samples')
    print(test_data.shape[0]), 'test'
    print train_label
    
    train_label = np_utils.to_categorical(train_label, 28) # divide into 28 categories
    test_label = np_utils.to_categorical(test_label, 28)

    model = Sequential()

    model.add(Convolution2D(16, 4, 4,
                            border_mode='valid',
                            input_shape=(1, 28, 28)))
    model.add(Activation('relu'))

    model.add(Convolution2D(32, 4, 4, border_mode='full'))
    model.add(Activation('relu'))

    model.add(Convolution2D(32, 4, 4, border_mode='full'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Convolution2D(64, 4, 4, border_mode='full'))
    model.add(Activation('relu'))

    model.add(Convolution2D(64, 4, 4, border_mode='full'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.5))

    model.add(Flatten())
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(28))
    model.add(Activation('softmax'))


    model.load_weights('model_weights.h5')
    model.compile(loss='categorical_crossentropy', optimizer='adadelta')

    model.fit(train_data, train_label, batch_size=100, nb_epoch=10, shuffle=True, verbose=1, show_accuracy=True, validation_data=(test_data, test_label))


    json_string  = model.to_json()


    open('model_architecture.json','w').write(json_string)
    model.save_weights('model_weights.h5')
コード例 #24
0
ファイル: model.py プロジェクト: zhouyu/Keras-genomics
def try_params( n_iterations, params, data=None, datamode='memory'):

	print "iterations:", n_iterations
	print_params( params )

        batchsize = 100
        if datamode == 'memory':
            X_train, Y_train = data['train']
            X_valid, Y_valid = data['valid']
            inputshape = X_train.shape[1:]
        else:
            train_generator = data['train']['gen_func'](batchsize, data['train']['path'])
            valid_generator = data['valid']['gen_func'](batchsize, data['valid']['path'])
            train_epoch_step = data['train']['n_sample'] / batchsize
            valid_epoch_step = data['valid']['n_sample'] / batchsize
            inputshape = data['train']['gen_func'](batchsize, data['train']['path']).next()[0].shape[1:]

        model = Sequential()
	model.add(Conv2D(128, (1, 24), padding='same', input_shape=inputshape, activation='relu'))
        model.add(GlobalMaxPooling2D())

        model.add(Dense(32,activation='relu'))
        model.add(Dropout(params['DROPOUT']))
        model.add(Dense(2))
        model.add(Activation('softmax'))

        optim = Adadelta
        myoptimizer = optim(epsilon=params['DELTA'], rho=params['MOMENT'])
        mylossfunc = 'categorical_crossentropy'
        model.compile(loss=mylossfunc, optimizer=myoptimizer,metrics=['accuracy'])

        early_stopping = EarlyStopping( monitor = 'val_loss', patience = 3, verbose = 0 )

        if datamode == 'memory':
            model.fit(
                    X_train,
                    Y_train,
                    batch_size=batchsize,
                    epochs=int( round( n_iterations )),
                    validation_data=(X_valid, Y_valid),
                    callbacks = [ early_stopping ])
            score, acc = model.evaluate(X_valid,Y_valid)
        else:
            model.fit_generator(
                    train_generator,
                    steps_per_epoch=train_epoch_step,
                    epochs=int( round( n_iterations )),
                    validation_data=valid_generator,
                    validation_steps=valid_epoch_step,
                    callbacks = [ early_stopping ])
            score, acc = model.evaluate_generator(valid_generator, steps=valid_epoch_step)

	return { 'loss': score, 'model': (model.to_json(), optim, myoptimizer.get_config(), mylossfunc) }
コード例 #25
0
ファイル: test_topology.py プロジェクト: BlakePrice/keras
def test_constant_initializer_with_numpy():
    model = Sequential()
    model.add(Dense(2, input_shape=(3,),
                    kernel_initializer=Constant(np.ones((3, 2)))))
    model.add(Dense(3))
    model.compile(loss='mse', optimizer='sgd', metrics=['acc'])

    json_str = model.to_json()
    model_from_json(json_str).summary()

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str).summary()
コード例 #26
0
ファイル: ann_train.py プロジェクト: tegbert/NeuralNetToy01
class Toy01:
    """Sets up and trains the ANN using keras"""

    def __init__(self, xtrain, ytrain, results_dir):
        self.xtrain = xtrain
        self.ytrain = ytrain
        self.results_dir = results_dir
        # ANN model
        self.optimizer = optimizers.RMSprop(lr=0.005)
        self.loss = 'mse'
        self.metrics = 'accuracy'
        self.model = Sequential()
        self.model.add(Dense(output_dim=30, input_dim=2, init='uniform'))
        self.model.add(Activation('relu'))
        self.model.add(Dense(output_dim=30, init='uniform'))
        self.model.add(Activation('relu'))
        self.model.add(Dense(output_dim=20, init='uniform'))
        self.model.add(Activation('relu'))
        self.model.add(Dense(output_dim=1, init='uniform'))
        self.model.add(Activation('sigmoid'))
        # Compile the model
        self.model.compile(
            loss=self.loss,
            optimizer=self.optimizer,
            metrics=[self.metrics]
        )

    def process(self, nepochs):
        progress_fname = os.path.join(self.results_dir, 'progress.txt')
        model_fname = os.path.join(self.results_dir, 'ann_model.txt')
        weights_dir = os.path.join(self.results_dir, 'weights')
        if not os.path.exists(weights_dir):
            os.mkdir(weights_dir, 0o755)
        step = EpochInstrospector(self.model, self.optimizer, progress_fname, weights_dir)
        fout = open(progress_fname, 'w')
        fout.close()
        with open(model_fname, 'w') as fout:
            jmodel = self.model.to_json()
            fout.write(jmodel)
        self.model.fit(
            self.xtrain,
            self.ytrain,
            batch_size=30,
            nb_epoch=nepochs,
            verbose=1,
            callbacks=[step],
            validation_split=0.0,
            validation_data=None,
            shuffle=True,
            class_weight=None,
            sample_weight=None
        )
コード例 #27
0
def test_TimeDistributed():
    # first, test with Dense layer
    model = Sequential()
    model.add(wrappers.TimeDistributed(core.Dense(2), input_shape=(3, 4)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')
    model.fit(np.random.random((10, 3, 4)), np.random.random((10, 3, 2)), nb_epoch=1, batch_size=10)

    # test config
    model.get_config()

    # compare to TimeDistributedDense
    test_input = np.random.random((1, 3, 4))
    test_output = model.predict(test_input)
    weights = model.layers[0].get_weights()

    reference = Sequential()
    reference.add(core.TimeDistributedDense(2, input_shape=(3, 4), weights=weights))
    reference.add(core.Activation('relu'))
    reference.compile(optimizer='rmsprop', loss='mse')

    reference_output = reference.predict(test_input)
    assert_allclose(test_output, reference_output, atol=1e-05)

    # test when specifying a batch_input_shape
    reference = Sequential()
    reference.add(core.TimeDistributedDense(2, batch_input_shape=(1, 3, 4), weights=weights))
    reference.add(core.Activation('relu'))
    reference.compile(optimizer='rmsprop', loss='mse')

    reference_output = reference.predict(test_input)
    assert_allclose(test_output, reference_output, atol=1e-05)

    # test with Convolution2D
    model = Sequential()
    model.add(wrappers.TimeDistributed(convolutional.Convolution2D(5, 2, 2, border_mode='same'), input_shape=(2, 3, 4, 4)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')
    model.train_on_batch(np.random.random((1, 2, 3, 4, 4)), np.random.random((1, 2, 5, 4, 4)))

    model = model_from_json(model.to_json())
    model.summary()

    # test stacked layers
    model = Sequential()
    model.add(wrappers.TimeDistributed(core.Dense(2), input_shape=(3, 4)))
    model.add(wrappers.TimeDistributed(core.Dense(3)))
    model.add(core.Activation('relu'))
    model.compile(optimizer='rmsprop', loss='mse')

    model.fit(np.random.random((10, 3, 4)), np.random.random((10, 3, 3)), nb_epoch=1, batch_size=10)
コード例 #28
0
ファイル: keras-train.py プロジェクト: shazh-ms/cleartk
def main(args):
    if len(args) < 1:
        sys.stderr.write("Error - one required argument: <data directory>\n")
        sys.exit(-1)

    working_dir = args[0]
    
    print("Reading data from %s..." % working_dir)
    
    Y, X = ctk_io.read_liblinear(working_dir)
        
    num_outputs = Y.shape[-1]
    num_examples, dimension = X.shape
    num_y_examples, num_labels = Y.shape
    assert num_examples == num_y_examples
    
    model = Sequential()
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    
    # Dense(10) is a fully-connected layer with 10 hidden units.
    # in the first layer, you must specify the expected input data shape:
    # here, 20-dimensional vectors.
    model.add(Dense(10, input_dim=dimension, init='uniform'))
    model.add(Activation('relu'))
    
    model.add(Dense(10, init='uniform'))
    model.add(Activation('relu'))
    
    if num_outputs > 1:
        model.add(Dense(num_outputs, init='uniform'))
        model.add(Activation('softmax'))                
        model.compile(loss='categorical_crossentropy',
                      optimizer=sgd,
                      metrics=['accuracy'])
    else:
        model.add(Dense(1, init='uniform'))
        model.add(Activation('sigmoid'))
        model.compile(loss='binary_crossentropy',
                      optimizer=sgd,  
                      metrics=['accuracy'])
    print("Shape of y is %s, shape of X is %s, max value in y is %f and min is %f with %d outcomes" % (str(Y.shape), str(X.shape), Y.max(), Y.min(), num_outputs) )
    model.fit(X, Y,
                  nb_epoch=nb_epoch,
                  batch_size=batch_size,
                  verbose=1)
                  
    model.summary()
    
    json_string = model.to_json()
    open(os.path.join(working_dir, 'model_0.json'), 'w').write(json_string)
    model.save_weights(os.path.join(working_dir, 'model_0.h5'), overwrite=True)
コード例 #29
0
ファイル: aligner_keras.py プロジェクト: wongjingping/whale
def train_xy(epochs=50, batch_size=32, h=256, w=256, ch=3, train_p=0.8, valid_p=0.1):
    print("Compiling Model")
    t_comp = time()
    model = Sequential()
    # reshape input to ch, h, w (no sample axis)
    model.add(Reshape(dims=(h, w, ch), input_shape=(ch * h * w,)))
    model.add(Permute((3, 1, 2)))
    # add conv layers
    model.add(Convolution2D(16, 3, 3, init="glorot_uniform", activation="relu", subsample=(1, 1)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(32, 3, 3, init="glorot_uniform", activation="relu", subsample=(1, 1)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 3, 3, init="glorot_uniform", activation="relu", subsample=(1, 1)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 3, 3, init="glorot_uniform", activation="relu", subsample=(1, 1)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(output_dim=2000, init="glorot_uniform", activation="relu", W_regularizer=l2(0.001)))
    model.add(Dropout(0.5))
    model.add(Dense(output_dim=2000, init="glorot_uniform", activation="relu", W_regularizer=l2(0.001)))
    model.add(Dropout(0.5))
    model.add(Dense(output_dim=4, init="glorot_uniform", activation="relu"))
    model.compile(optimizer="rmsprop", loss="mse")
    t_train = time()
    print("Took %.1fs" % (t_train - t_comp))
    # split dataset
    i_test = int(train_p * nrow) / batch_size * batch_size
    i_valid = int(i_test * (1 - valid_p)) / batch_size * batch_size
    X_train, Y_train = X_[:i_valid,], Y_[:i_valid,]
    X_valid, Y_valid = X_[i_valid:i_test,], Y_[i_valid:i_test,]

    # naive fitting to lower rmse faster
    hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=10, verbose=1, validation_split=0.1)
    print(hist)
    # fit by batch using generator!
    img_aug = image_augmentor(X_, Y_, i_valid)
    hist = model.fit_generator(
        generator=img_aug,
        samples_per_epoch=i_valid,
        nb_epoch=5000,
        verbose=1,
        validation_data=(X_valid, Y_valid),
        nb_worker=1,
    )
    rmse_test = model.evaluate(X_[i_test:,], Y_[i_test:,])
    print("Test RMSE: %.4f" % rmse_test)

    # save model
    model_json = model.to_json()
    open(path_img + "locate/model_116.json", "w").write(model_json)
    model.save_weights(path_img + "locate/model_116_weights.h5")
コード例 #30
0
ファイル: words.py プロジェクト: AronMiller/NNPlayground
def buildModel():
	# build the model: 2 stacked LSTM
	print('Build model...')
	model = Sequential()

	# model.add(Dense(512, input_dim= maxlen * len(chars), init='uniform'))
	# model.add(Activation('relu'))
	# model.add(Dropout(0.1))
	'''
	model.add(Dense(256, input_dim= maxlen * len(chars), init='uniform'))
	model.add(Activation('relu'))
	model.add(Dropout(0.1))

	model.add(Dense(128, input_dim= maxlen * len(chars), init='uniform'))
	model.add(Activation('relu'))
	model.add(Dropout(0.1))

	model.add(Dense(64, input_dim= maxlen * len(chars), init='uniform'))
	model.add(Activation('relu'))
	model.add(Dropout(0.1))
	'''

	# model.add(LSTM(9, return_sequences=False, input_shape=(maxlen, len(chars))))
	model.add(LSTM(32, return_sequences=True, input_shape=(maxlen, len(chars))))
	model.add(Dropout(0.1))

	model.add(LSTM(32, return_sequences=False))
	model.add(Dropout(0.1))

	model.add(Dense(32, init='uniform'))
	model.add(Activation('relu'))
	model.add(Dropout(0.1))

	#model.add(LSTM(32, return_sequences=True))
	#model.add(Dropout(0.1))
	#model.add(LSTM(32, return_sequences=False))
	#model.add(Dropout(.1))
	model.add(Dense(len(chars)))
	model.add(Activation('softmax'))

	model.compile(loss='categorical_crossentropy', optimizer='rmsprop')


	# Save model
	json_string = model.to_json()
	model_file = open(output_folder + "model.json", "w+")
	model_file.write(json_string)
	model_file.close()

	return model
コード例 #31
0
ファイル: owneuralnet.py プロジェクト: RachitKansal/NeuralNet
class OWNeuralNet(widget.OWWidget):
    # Widget needs a name, or it is considered an abstract widget
    # and not shown in the menu.
    name = "MLP Multi-Class"
    description = "Generates a MLP model from data."
    icon = "icons/mywidget.svg"

    # Takes the Orange data Table as input and produces a learner output
    # which is sccepted by the predictor widget
    inputs = [("Data", Orange.data.Table, "set_data")]
    outputs = [("Predictor", NetLearner)]

    # setting the values for various combo-boxes and the checkbox
    # stores the number of layers
    num_layers = settings.Setting(0)

    # these values store the no. of neurons in each layer
    neuron_lyr1 = settings.Setting(0)
    neuron_lyr2 = settings.Setting(0)
    neuron_lyr3 = settings.Setting(0)
    neuron_lyr4 = settings.Setting(0)
    # These store the index of selected activation function in comboboxes
    activation_lyr1 = settings.Setting(0)
    activation_lyr2 = settings.Setting(0)
    activation_lyr3 = settings.Setting(0)
    activation_lyr4 = settings.Setting(0)

    # The different activation functions available
    activations = ['sigmoid', 'tanh', 'linear']
    # The different kind of loss functions available to the user
    lossfunctions = ['mean_squared_error', 'mean_squared_logarithmic_error', \
    'categorical_crossentropy', 'poisson']
    activation_out = settings.Setting(0)
    loss_function = settings.Setting(0)
    # Stores the selected learning rate
    learning_rate = settings.Setting(0)
    # stores the selected number of iterations to perform
    iteration_val = settings.Setting(0)
    # stores whether to perform Normalization on data or not
    checkbox_val = settings.Setting(0)
    # the percentage of data to be used for cross validation
    validation = settings.Setting(0)
    # the batchsize selected for stochastic gradient descent
    batchsize = settings.Setting(0)

    save_dir = settings.Setting("")

    def __init__(self):
        super().__init__()

        self.dataset = None

        # The Information Box
        self.infoBox = gui.widgetBox(self.controlArea, "Info", spacing=1)
        self.infoa = gui.widgetLabel(
            self.infoBox, 'No data on input yet, waiting to get something.')
        self.infob = gui.widgetLabel(self.infoBox, '')
        self.infoc = gui.widgetLabel(self.infoBox, '')
        gui.radioButtons(self.infoBox, self, 'num_layers', btnLabels=('1', '2', '3', '4'), \
            label = 'Number of Layers: ', orientation='horizontal', callback = self.updateLayer)

        # The Layer Box: provides all the options for all 4 layers
        self.layerBox = gui.widgetBox(self.controlArea, "Layer Options", spacing=1, \
            orientation='horizontal')

        self.layerBox1 = gui.widgetBox(self.layerBox, spacing=1)
        self.layerBox2 = gui.widgetBox(self.layerBox, spacing=1)

        self.box = []
        for i in range(4):
            if (i < 2):
                self.box.append(
                    gui.widgetBox(self.layerBox1, 'Layer ' + str(i + 1)))
            else:
                self.box.append(
                    gui.widgetBox(self.layerBox2, 'Layer ' + str(i + 1)))
            gui.spin(self.box[i], self, 'neuron_lyr' + str(i+1), minv=1, maxv=50, step=1, \
                label='Neurons in hidden Layer ' + str(i+1) + ': ')
            combo = gui.comboBox(self.box[i], self, 'activation_lyr' + str(i+1), \
                label = 'Activation of Layer ' + str(i+1) + ':', \
                items=('sigmoid', 'tanh', 'linear'))
            self.box[i].setDisabled(True)
        self.layerBox.setDisabled(True)

        # The Options Box: gives some general options to configure the network
        self.optionsBox = gui.widgetBox(self.controlArea, "General Options", \
            orientation='vertical', spacing=1)
        self.generalBox = gui.widgetBox(self.optionsBox,
                                        orientation='horizontal',
                                        spacing=1)
        self.optionsBox1 = gui.widgetBox(self.generalBox, spacing=1)
        self.optionsBox2 = gui.widgetBox(self.generalBox, spacing=1)

        gui.spin(self.optionsBox1, self, 'learning_rate', minv=0.01, maxv=1.0, \
            step=0.01, label='Learning Rate: ', spinType=float)
        combo = gui.comboBox(self.optionsBox1, self, 'activation_out', \
            label = 'Ouput Layer Activation: ', orientation='horizontal', items=('sigmoid', 'tanh', 'linear'))
        gui.spin(self.optionsBox1, self, 'validation', minv=0.01, maxv=0.2, \
            step=0.01, label='Validation Split: ', spinType=float)
        gui.spin(self.optionsBox2, self, 'iteration_val', minv=1, maxv=100, \
            step=1, label='Iterations: ')
        combo = gui.comboBox(self.optionsBox2, self, 'loss_function', label = 'Loss Function: ', \
            orientation='horizontal', items=('Mean Squared Error', 'Mean Squared Log Error', \
                'Categorical Cross-Entropy', 'Poisson'))
        self.batch_spin = gui.spin(self.optionsBox2, self, 'batchsize', minv=1, maxv=300, \
            step=1, label='Batch Size: ')

        # The GUI for the Progress Bar
        self.progress = QtGui.QProgressBar(self.optionsBox)
        gui.miscellanea(self.progress, None, self.optionsBox)

        # The GUI for checkbox of normalizing the data
        gui.checkBox(self.optionsBox,
                     self,
                     'checkbox_val',
                     label='Normalize Data')

        self.buttonBox = gui.widgetBox(self.optionsBox, spacing=1)

        # The GUI for the apply button
        self.apply_button = gui.button(self.buttonBox, self, "Apply", callback = self.apply, \
            default=False, autoDefault=False)

        # The GUI for the save button
        self.save_button = gui.button(self.buttonBox, self, "Save", callback = self.save, \
            default=False, autoDefault=False)
        self.optionsBox.setDisabled(True)

    def updateLayer(self):
        """ This function is used to control the GUI of the widget, 
        in particular the layerBox for the widget"""
        if self.num_layers == 0:
            self.box[0].setDisabled(False)
            for i in range(1, 4):
                self.box[i].setDisabled(True)
        elif self.num_layers == 1:
            self.box[0].setDisabled(False)
            self.box[1].setDisabled(False)
            for i in range(2, 4):
                self.box[i].setDisabled(True)
        elif self.num_layers == 2:
            self.box[0].setDisabled(False)
            self.box[1].setDisabled(False)
            self.box[2].setDisabled(False)
            self.box[3].setDisabled(True)
        else:
            self.box[0].setDisabled(False)
            self.box[1].setDisabled(False)
            self.box[2].setDisabled(False)
            self.box[3].setDisabled(False)

    def set_data(self, dataset):
        """ This function is called whevever the data is input into the widget,
        It also controls some GUI aspects of the widget`"""
        if dataset is not None:
            self.infoa.setText('%d instances in input data set' % len(dataset))
            self.infob.setText('%d attributes in input data set' %
                               len(dataset.domain.attributes))
            # Limited the batch size between 0.005 to 0.025, in
            # order tk=o make training fats and also accurate
            if (len(dataset) >= 200):
                self.batchsize = int(0.005 * len(dataset))
                self.batch_spin.setMinimum(int(0.005 * len(dataset)))
                self.batch_spin.setMaximum(int(0.025 * len(dataset)))
            else:
                # here the dataset is to small, hence fixed the
                # batch size programmatically
                self.batchsize = 1
                self.batch_spin.setMinimum(1)
                self.batch_spin.setMaximum(10)
            self.optionsBox.setDisabled(False)
            self.layerBox.setDisabled(False)
            self.updateLayer()
            self.dataset = dataset
            self.save_button.setDisabled(True)

        else:
            self.infoa.setText(
                'No data on input yet, waiting to get something.')
            self.infob.setText('')
            self.optionsBox.setDisabled(True)
            self.layerBox.setDisabled(True)
            self.dataset = None

    def apply(self):
        # The action for apply button
        self.buttonBox.setDisabled(True)
        self.save_button.setDisabled(False)

        # Taking care of normalizatio of data
        if (self.checkbox_val == True):
            dataX = normalize(self.dataset.X.astype(float))
        else:
            dataX = self.dataset.X.astype(float)

        # Setting the range of progress bar to number of iteration
        # and hence makes iot accurate
        self.progress.setRange(0, self.iteration_val)

        train_data = dataX.astype(float)

        # Preparing the target for multilabel classification
        target = self.dataset.Y.astype(int)
        train_target = []
        unique_target = np.unique(target)
        for i in range(0, len(target)):
            dummy = []
            k = 0
            for j in unique_target:
                if j == target[i]:
                    dummy.insert(k, 1)
                else:
                    dummy.insert(k, 0)
                k = k + 1
            train_target.insert(i, dummy)
        train_target = np.array(train_target)

        # Building the model for a feedforard network
        self.model = Sequential()
        self.model.add(Dense(input_dim = len(train_data[0]), output_dim = self.neuron_lyr1, \
            init = 'uniform', activation = self.activations[self.activation_lyr1]))
        if self.num_layers == 0:
            self.model.add(Dense(output_dim = len(train_target[0]), init = 'uniform', \
                activation = self.activations[self.activation_out]))
        elif self.num_layers == 1:
            self.model.add(Dense(output_dim = self.neuron_lyr2, init = 'uniform', \
                activation = self.activations[self.activation_lyr2]))
            self.model.add(Dense(output_dim = len(train_target[0]), init = 'uniform', \
                activation = self.activations[self.activation_out]))
        else:
            self.model.add(Dense(output_dim = self.neuron_lyr2, init = 'uniform', \
                activation = self.activations[self.activation_lyr2]))
            self.model.add(Dense(output_dim = self.neuron_lyr3, init = 'uniform', \
                activation = self.activations[self.activation_lyr3]))
            self.model.add(Dense(output_dim = len(train_target[0]), init = 'uniform', \
                activation = self.activations[self.activation_out]))

        # the stochastic gradient descent optimizer
        sgd = SGD(lr=self.learning_rate)

        self.model.compile(loss=self.lossfunctions[self.loss_function],
                           optimizer=sgd)

        # Using multi-threading to train the model
        self.thread = MyThread(self.model, self.progress, train_data, train_target, \
            self.iteration_val, self.validation, self.batchsize)
        self.thread.trigger.connect(self.update_progress)
        self.thread.start()

        # Sending the output of the model
        self.send("Predictor", self.model)

    def save(self):
        # The functionality of the save button for the model
        filename = QtGui.QFileDialog.getSaveFileName(self, "Save Model",
                                                     self.save_dir)
        self.save_dir = os.path.dirname(filename)
        json_string = self.model.to_json()
        open(filename + '.json', 'w').write(json_string)
        self.model.save_weights(filename + 'h5')

    def update_progress(self, value, accuracy):
        # Updates the progress bar for every iteration during training
        self.progress.setValue(value)
        if (value == 0):
            self.buttonBox.setDisabled(False)
        if (accuracy != -1):
            self.infoc.setText("Validation Accuracy: " +
                               str(round(accuracy, 3)))
コード例 #32
0
    Recall = recall_score(y_test, y_pred, average="macro")

    #print("Accuaracy: ",accuracy_score(y_test, y_pred))
    #print("f1 score: ",f1_score(y_test, y_pred, average="macro"))
    #print("Precision: ",precision_score(y_test, y_pred, average="macro"))
    #print("Recall: ",recall_score(y_test, y_pred, average="macro"))
    #print(CM)

    Sum = 0
    Sum = Sum + abs(dfCor[feat[0]].corr(dfCor[feat[1]]).round(2))
    Sum = Sum + abs(dfCor[feat[0]].corr(dfCor[feat[2]]).round(2))
    Sum = Sum + abs(dfCor[feat[1]].corr(dfCor[feat[2]]).round(2))
    AvgCor = Sum / 3

    # serialize model to JSON
    model_json = CNN1.to_json()
    Name = ""
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
        # serialize weights to HDF5
        Name = "CNN" + str(Count) + ".h5"
        CNN1.save_weights(Name)
        print("Saved model to disk: " + Name)
    Count += 1
    ModelDict[Name] = {
        "Components": feat,
        "CM": CM,
        "AvgCor": AvgCor,
        "Accuraccy": Accuracy,
        "F1": F1,
        "Precision": Precision,
コード例 #33
0
def train():

    model = Sequential()

    X_train = np.load(home + '/gabor/numpyFiles/Training Set.npy')
    X_test = np.load(home + '/gabor/numpyFiles/TestSet.npy')
    Y_train = np.load(home + '/gabor/numpyFiles/Training Labels.npy')
    Y_test = np.load(home + '/gabor/numpyFiles/TestSet Labels.npy')

    #X_test = X_test.reshape(-1, 1, 30, 96)
    Y_test = np_utils.to_categorical(Y_test, 447)

    #X_train = X_train.reshape(-1, 1, 30, 96)
    Y_train = np_utils.to_categorical(Y_train, 447)

    print("X_test.shape == {};".format(X_test.shape))
    print("Y_test.shape == {};".format(Y_test.shape))
    print("X_test.shape == {};".format(X_train.shape))
    print("Y_test.shape == {};".format(Y_train.shape))

    nb_hidden_layers = [len(X_train[0]), 700, 500, 300]

    XtrainKeras = X_train
    print 'shape of XTrain Keras is ', XtrainKeras.shape
    YtrainKeras = np_utils.to_categorical(Y_train, nb_classes)
    op1 = RMSprop(lr=0.01, rho=0.5, epsilon=1e-8)

    X_train_tmp = XtrainKeras
    trained_encoders = []

    #XtrainKeras=XwhiteTrain.reshape(-1,1,len(XwhiteTrain),len(XwhiteTrain[0]))
    #YtrainKeras=np_utils.to_categorical(Y_train, nb_classes)

    #XtestKeras=X_test.reshape(-1,1,imageWidth,imageHeight)
    #YtestKeras=np_utils.to_categorical(Y_test, nb_classes)
    #X_train_tmp=XtrainKeras

    for n_in, n_out in zip(nb_hidden_layers[:-1], nb_hidden_layers[1:]):
        print('Pre-training the layer: Input {} -> Output {}'.format(
            n_in, n_out))
        # Create AE and training
        ae = Sequential()
        encoder = containers.Sequential(
            [Dense(n_out, input_dim=n_in, activation='sigmoid')])
        decoder = containers.Sequential(
            [Dense(n_in, input_dim=n_out, activation='sigmoid')])
        ae.add(
            AutoEncoder(encoder=encoder,
                        decoder=decoder,
                        output_reconstruction=False))
        ae.compile(loss='mean_squared_error', optimizer=op1)
        hist = ae.fit(X_train_tmp,
                      X_train_tmp,
                      batch_size=batch_size,
                      nb_epoch=nb_epoch)
        print(hist.history)
        Fname = prefix + 'autoencoder_n_in=' + str(n_in) + '_n_out= ' + str(
            n_out) + '.json'
        weightName = prefix + 'Weights_autoencoder_n_in=' + str(
            n_in) + '_n_out= ' + str(n_out) + '.h5'
        json_string = model.to_json()
        open(Fname, 'w').write(json_string)
        model.save_weights(weightName, overwrite=True)
        # Store trainined weight
        trained_encoders.append(ae.layers[0].encoder)
        # Update training data
        X_train_tmp = ae.predict(X_train_tmp)

    #ae1=Sequential()
    #encoder1=containers.Sequential([Dense(len(XwhiteTrain[0])-200,len(XwhiteTrain[0]),activation='sigmoid')])

    Y_test = np_utils.to_categorical(Y_test, nb_classes)
    #X_test=X_test.reshape(-1,len(X_test[0]))
    print 'shape of X_test  is ', X_test.shape
    print('Fine-tuning')
    sgd = SGD(lr=0.01, momentum=0.5, decay=0., nesterov=False)

    i = 1
    model = Sequential()
    for encoder in trained_encoders:
        model.add(encoder)
    model.add(
        Dense(nb_classes, input_dim=nb_hidden_layers[-1],
              activation='softmax'))

    model.compile(loss='categorical_crossentropy', optimizer=sgd)

    hist = model.fit(XtrainKeras,
                     YtrainKeras,
                     batch_size=batch_size,
                     nb_epoch=nb_epoch,
                     show_accuracy=True,
                     validation_data=(X_test, Y_test))
    score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])
    Fname = prefix + '2 FineTuning_model=' + '.json'
    weightName = prefix + 'Fine Tunes Weights_autoencoder_i=' + str(i) + '.h5'
    json_string = model.to_json()
    open(Fname, 'w').write(json_string)
    model.save_weights(weightName, overwrite=True)
def model10(X_train, y_train,X_test, y_test,in_batch_size=100,in_epochs=10,model_json_file="model.json",model_h5_file="model.h5"): # RNN: Recurrent Neural Networks
    # LSTM
    
    # Convert labels to categorical one-hot encoding  
    y_train_one_hot_labels = to_categorical(y_train, num_classes=n_classes)
    y_test_one_hot_labels = to_categorical(y_test, num_classes=n_classes)

    # create the model    
    model = Sequential()
    model.add(Embedding(num_letters, output_dim=fixed_seq_length))
    model.add(LSTM(75,return_sequences=True))
    model.add(LSTM(25))
    model.add(Dropout(0.25))
    model.add(Dense(16, input_dim=fixed_seq_length, kernel_initializer='uniform', activation='relu'))
    model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
    model.add(Dense(n_classes, kernel_initializer='uniform', activation='softmax'))
    
    #opt = 'rmsprop'
    #opt = optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
    #opt = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=0.001, decay=0.0)
    #opt = optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
    #opt = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=None, decay=0.0)
    opt = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
    #opt = optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
    #opt = optimizers.Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, schedule_decay=0.004)

    model.compile(loss='categorical_crossentropy',optimizer=opt, metrics=['accuracy'])
    print(model.summary())

    # fit the model
    history = model.fit(X_train, y_train_one_hot_labels, epochs=in_epochs, batch_size=in_batch_size,validation_data=(X_test, y_test_one_hot_labels), verbose=1)

    # evaluate the model
    train_loss, train_acc = model.evaluate(X_train, y_train_one_hot_labels, verbose=0)
    test_loss, test_acc = model.evaluate(X_test, y_test_one_hot_labels, verbose=0)
    print('Loss     ======= Train: %.3f, Test: %.3f' % (train_loss, test_loss))
    print('Accuracy ======= Train: %.3f, Test: %.3f' % (train_acc, test_acc))

    # summarize history for accuracy
    pyplot.plot(history.history['acc'])
    pyplot.plot(history.history['val_acc'])
    pyplot.title('model accuracy')
    pyplot.ylabel('accuracy')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'test'], loc='upper left')
    #pyplot.show()
    pyplot.savefig('module10_accuracy.png')
    # summarize history for loss
    pyplot.plot(history.history['loss'])
    pyplot.plot(history.history['val_loss'])
    pyplot.title('model loss')
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'test'], loc='upper left')
    #pyplot.show()
    pyplot.savefig('module10_loss.png')
    
    # serialize model to JSON
    model_json = model.to_json()
    with open(model_json_file, "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(model_h5_file)
    print("Saved model to disk")

    return()
コード例 #35
0
model.summary()

filepath="/home/prajval/end_to_end_learning/src/diy_driverless_car_ROS-master/rover_ml/output/weights/weights-improvement-{epoch:02d}-{val_loss:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto', period=1)

early_stop = EarlyStopping(monitor='val_loss', patience=10)

callbacks_list = [checkpoint, early_stop]

# Fit the model
history_object = model.fit_generator(train_generator, steps_per_epoch=(len(train_samples) / batch_size_value), validation_data=validation_generator, validation_steps=(len(validation_samples)/batch_size_value), callbacks=callbacks_list, epochs=n_epoch)

# Save model
model.save('/home/prajval/end_to_end_learning/src/diy_driverless_car_ROS-master/rover_ml/output/weights/model.h5')
with open('/home/prajval/end_to_end_learning/src/diy_driverless_car_ROS-master/rover_ml/output/weights/model.json', 'w') as output_json:
    output_json.write(model.to_json())

# Save TensorFlow model
tf.train.write_graph(K.get_session().graph.as_graph_def(), logdir='.', name='model.pb', as_text=False)

# Plot the training and validation loss for each epoch
print('Generating loss chart...')
plt.plot(history_object.history['loss'])
plt.plot(history_object.history['val_loss'])
plt.title('model mean squared error loss')
plt.ylabel('mean squared error loss')
plt.xlabel('epoch')
plt.legend(['training set', 'validation set'], loc='upper right')
plt.savefig('/home/prajval/end_to_end_learning/src/diy_driverless_car_ROS-master/rover_ml/output/weights/model.png')

# Done
コード例 #36
0
                                                    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')

history = model_2.fit_generator(train_generator,
                                steps_per_epoch=nb_train_samples // batch_size,
                                epochs=epochs,
                                validation_data=validation_generator,
                                validation_steps=nb_validation_samples //
                                batch_size)

# serialize weights to HDF5
model_2.save_weights('first_tryAfterAdding.h5')

# serialize model to JSON
model_json = model_2.to_json()
with open("modelafterAdding.json", "w") as json_file:
    json_file.write(model_json)

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
コード例 #37
0
    [x1, x2],
    y=y,
    batch_size=batch_size,
    epochs=num_epochs,
    verbose=1,
    validation_split=0.1,
    shuffle=True,
    callbacks=[checkpoint, checkpoint_board,
               checkpoint_early])  # Add tensorboard in callbacks list

pickle.dump(history.history, open(output + 'history.p', 'wb'))

# Save Model

# serialize model to JSON
model_json = merged_model.to_json()
with open(output + "model.json", "w") as json_file:
    json_file.write(model_json)

# In[ ]:

# Make predictions on submission data

# Import test data
csv_test = data_loc + 'test.csv'
test_data = pd.read_csv(csv_test)

t_x1 = tk.texts_to_sequences(test_data.question1.values.astype(str))
t_x1 = sequence.pad_sequences(t_x1, maxlen=max_len)

t_x2 = tk.texts_to_sequences(test_data.question2.values.astype(str))
コード例 #38
0
# Testing on new data

import numpy as np
from keras.preprocessing import image

test_image = image.load_img('data/new-test-images/milhouse_impossible.jpg',
                            target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)

result = classifier.predict(test_image)
training_set.class_indices

# Saving model
classifier_json = classifier.to_json()
with open("classifier.json", "w") as json_file:
    json_file.write(classifier_json)

classifier.save_weights("classifier.h5")
print("saved model to disk")

# Loading model
from keras.models import model_from_json
json_file = open('classifier.json', 'r')
loaded_classifier_json = json_file.read()
json_file.close()
loaded_classifier = model_from_json(loaded_classifier_json)
loaded_classifier.load_weights("classifier.h5")
print("loaded model from disk")
コード例 #39
0
ファイル: train_keras_model.py プロジェクト: wsylt/OCRSys
def train(cwsInfo, cwsData, modelPath, weightPath):

    (initProb, tranProb), (vocab, indexVocab) = cwsInfo
    (X, y) = cwsData

    train_X, test_X, train_y, test_y = model_selection.train_test_split(
        X, y, train_size=0.9, random_state=1)

    train_X = np.array(train_X)
    train_y = np.array(train_y)
    test_X = np.array(test_X)
    test_y = np.array(test_y)

    outputDims = len(cws.corpus_tags)
    Y_train = np_utils.to_categorical(train_y, outputDims)
    Y_test = np_utils.to_categorical(test_y, outputDims)
    batchSize = 128
    vocabSize = len(vocab) + 1
    wordDims = 100
    maxlen = 7
    hiddenDims = 100

    w2vModel = Word2Vec.load('./word2vec_model/msr_training_word2vec.model')
    embeddingDim = w2vModel.vector_size
    embeddingUnknown = [0 for i in range(embeddingDim)]
    embeddingWeights = np.zeros((vocabSize + 1, embeddingDim))
    for word, index in vocab.items():
        if word in w2vModel:
            e = w2vModel[word]
        else:
            e = embeddingUnknown
        embeddingWeights[index, :] = e

    #LSTM
    model = Sequential()
    model.add(
        Embedding(output_dim=embeddingDim,
                  input_dim=vocabSize + 1,
                  input_length=maxlen,
                  mask_zero=True,
                  weights=[embeddingWeights]))
    model.add(LSTM(output_dim=hiddenDims, return_sequences=True))
    model.add(LSTM(output_dim=hiddenDims, return_sequences=False))
    model.add(Dropout(0.5))
    model.add(Dense(outputDims))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=["accuracy"])

    result = model.fit(train_X,
                       Y_train,
                       batch_size=batchSize,
                       nb_epoch=20,
                       validation_data=(test_X, Y_test))

    j = model.to_json()
    fd = open(modelPath, 'w')
    fd.write(j)
    fd.close()

    model.save_weights(weightPath)

    return model
コード例 #40
0
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np
#Compute probabilities
Y_pred = nn.predict(x_test)
#Assign most probable label
y_pred = np.argmax(Y_pred, axis=1)

#Plot statistics
print('Analysis of results')
target_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
print(
    classification_report(np.argmax(y_test, axis=1),
                          y_pred,
                          target_names=target_names))
print(confusion_matrix(np.argmax(y_test, axis=1), y_pred))

#Saving model and weights
from keras.models import model_from_json
nn_json = nn.to_json()
with open('nn.json', 'w') as json_file:
    json_file.write(nn_json)
weights_file = "weights-MNIST_" + str(score[1]) + ".hdf5"
nn.save_weights(weights_file, overwrite=True)

#Loading model and weights
#json_file = open('nn.json','r')
#nn_json = json_file.read()
#json_file.close()
#nn = model_from_json(nn_json)
#nn.load_weights(weights_file)
コード例 #41
0
ファイル: handwrite.py プロジェクト: junhuizx/ai
model.add(
    Convolution2D(nb_filter=64,
                  nb_row=3,
                  nb_col=3,
                  dim_ordering="th",
                  border_mode="same",
                  bias=False,
                  init="uniform"))
model.add(AveragePooling2D(pool_size=(2, 2), dim_ordering="th"))
model.add(Flatten())
model.add(Dense(output_dim=1000, activation="sigmoid"))
model.add(Dense(output_dim=1000, activation="sigmoid"))
model.add(Dense(output_dim=10, activation="linear"))

with open("digits_model.json", "w") as f:
    f.write(model.to_json())

plot(model, to_file="digits_model.png", show_shapes=True)

model.to_json()

opt = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="mse", optimizer=opt, metrics=["accuracy"])

model.fit(train_digits,
          train_labels_one_hot,
          batch_size=32,
          nb_epoch=10,
          callbacks=[history])

model.save_weights("digits_model_weights.hdf5")
コード例 #42
0
ファイル: rnn.py プロジェクト: sailfish009/FMol
def train_rnn(config, data, tokens, number):
    """
    Train one RNN, keep the best weights of the model and stop it when it doesnt learn anymore

    :param config: config to use
    :type config: dict
    :param data: data to use to train the RNN
    :type data: list of list of str
    :param tokens: list of tokens used to train the RNN
    :type tokens: list of str
    :param number: id of the model
    :type number: int
    :return: None
    """
    print("Model : " + str(number))
    x_train, y_train = convert_data_to_numbers(tokens, data)

    print("SMILES converted to numbers")

    maxlen = 81

    x = sequence.pad_sequences(x_train, maxlen=maxlen, dtype='int32',
                               padding='post', truncating='pre', value=0.)
    y = sequence.pad_sequences(y_train, maxlen=maxlen, dtype='int32',
                               padding='post', truncating='pre', value=0.)

    print("Loading y_train_one_hot")

    y_train_one_hot = np.array([to_categorical(y_i, num_classes=len(tokens)) for y_i in y])
    print(y_train_one_hot.shape)

    n = x.shape[1]

    model = Sequential()

    model.add(Embedding(input_dim=len(tokens), output_dim=len(tokens), input_length=n, mask_zero=False))
    model.add(GRU(units=256, return_sequences=True, activation="tanh", input_shape=(81, 26)))
    model.add(Dropout(0.2))
    model.add(GRU(256, activation='tanh', return_sequences=True))
    model.add(Dropout(0.2))
    model.add(TimeDistributed(Dense(len(tokens), activation='softmax')))
    optimizer = Adam(lr=config['learning_rate'])
    model.summary()

    save_directory = 'rnn_models/' + config['configuration_name'] + '/'
    log = save_directory + 'log'
    model_weights = save_directory + 'model_weights_' + str(number) + '.h5'
    model_architecture = save_directory + 'model_architecture_' + str(number) + '.json'

    if not os.path.isdir(save_directory):
        os.mkdir(save_directory)
    if not os.path.isdir(log):
        os.mkdir(log)

    tensorboard = TensorBoard(log_dir=log + "/{}".format(str(datetime.datetime.now()) + '_model' + str(number)))
    early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')
    mcp_save = ModelCheckpoint(model_weights, save_best_only=True, monitor='val_loss', mode='min')

    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    model.fit(x, y_train_one_hot, epochs=config['epochs'], batch_size=512,
              validation_split=0.1, callbacks=[tensorboard, early_stopping, mcp_save])

    model_json = model.to_json()
    with open(model_architecture, "w") as json_file:
        json_file.write(model_json)
    print("Model saved")

    with open(save_directory + "config.json", 'w') as conf:
        json.dump(config, conf)
コード例 #43
0
# In[9]:

clf.summary()

# In[10]:

clf.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# In[11]:

tbCallBack = keras.callbacks.TensorBoard(
    log_dir='./Graph',
    histogram_freq=0,  # for tensorboard
    write_graph=True,
    write_images=True)
clf.fit(X_train, Y_train, batch_size=15, epochs=5, callbacks=[tbCallBack])

# In[12]:

score = clf.evaluate(X_test, Y_test, batch_size=128)
print('\nAnd the Score is ', score[1] * 100, '%')

# In[13]:

model_json = clf.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
clf.save_weights("model.h5")
print("Saved model to disk")
コード例 #44
0
class DenseNeuralNetwork:
    def __init__(self):
        self.model = None
        self.drp_rate = 0.1

    def learn(self,
              train_data,
              train_target,
              drp_rate=0.1,
              epochs=150,
              batch_size=5,
              num_class=2):

        # Trying to get consistent results but this is just the first step
        #   It looks like keras doesn't currenlty allow seeds to be initialized
        #   So every time there is new seed and random new weights so the results can be
        #       different on each run
        numpy.random.seed(0)

        self.model = Sequential()

        num_features = train_data.shape[1]
        # numpy.randomfrom keras.utils import to_categorical.seed(0)
        # Adding first dense layer with acitivation relu and dropout
        self.model.add(Dense(num_features + 1, input_dim=num_features))
        self.model.add(Activation('sigmoid'))

        self.model.add(Dense(num_features))
        self.model.add(Activation('sigmoid'))
        self.model.add(Dense(num_features))
        self.model.add(Activation('sigmoid'))

        self.model.add(Dense(num_features))
        self.model.add(Activation('sigmoid'))
        self.model.add(Dense(num_features))
        self.model.add(Activation('sigmoid'))

        # Adding final output layer with softmax
        self.model.add(Dense(units=num_class))
        self.model.add(Activation('softmax'))
        print np.unique(train_target)
        print train_target[0]
        y_classes = [
            np.argmax(train_target, axis=None, out=None) for y in train_target
        ]
        y_ints = [y.argmax() for y in train_target]
        #print y_classes.shape
        cw = class_weight.compute_class_weight('balanced', np.unique(y_ints),
                                               y_ints)

        print cw
        earlystop = EarlyStopping(monitor='val_loss', min_delta=0.00001, patience=955, \
                                  verbose=1, mode='auto')
        callbacks_list = [earlystop]
        callbacks_list = []
        self.model.compile(loss='categorical_crossentropy', optimizer=Adamax())
        self.model.fit(train_data,
                       train_target,
                       batch_size=batch_size,
                       validation_split=0.2,
                       epochs=epochs,
                       callbacks=callbacks_list,
                       verbose=0)  #{0:.5, 1:.6})

    def test(self, test_data, test_target):
        score = self.model.evaluate(test_data, test_target, batch_size=5)
        print('\n')
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])
        #print recall_score(test_target, self.model.predict(test_data))

    def save(self, name=""):
        f_name = "model/model-dnn"
        name = f_name + "-" + name
        model_json = self.model.to_json()
        with open(name + ".json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights(name + ".h5")
        print("Saved model to disk")

    def load(self, name=""):
        f_name = "model/model-dnn"
        name = f_name + "-" + name
        # load json and create model
        json_file = open(name + '.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        loaded_model = model_from_json(loaded_model_json)
        # load weights into new model
        loaded_model.load_weights(name + ".h5")
        print("Loaded model from disk")

        # evaluate loaded model on test data
        loaded_model.compile(loss='categorical_crossentropy',
                             optimizer=Adamax(),
                             metrics=['accuracy'])
        self.model = loaded_model

    def predict(self, test_data):
        prediction = self.model.predict(test_data)
        return prediction
コード例 #45
0
def main(batch_size, nb_epoch, train_flag, pickle_adr, save_address,
         number_of_classes, verbose):

    np.random.seed(1986)  # for reproducibility

    img_rows, img_cols = 47, 57
    nb_classes = number_of_classes

    if verbose:
        print 'save adress', save_address
        print 'train flag', train_flag

    if not os.path.exists(save_address):
        if verbose:
            print('making... ' + save_address)
        os.makedirs(save_address)

    dataset_name1 = 'img_5.0'
    ad1 = pickle_adr + dataset_name1 + '_class' + str(
        nb_classes) + '_norm.pkl.gz'

    dataset_name2 = 'img_4.0'
    ad2 = pickle_adr + dataset_name2 + '_class' + str(
        nb_classes) + '_norm.pkl.gz'

    dataset_name3 = 'img_1.0'
    ad3 = pickle_adr + dataset_name3 + '_class' + str(
        nb_classes) + '_norm.pkl.gz'

    dataset_name4 = 'img_2.0'
    ad4 = pickle_adr + dataset_name4 + '_class' + str(
        nb_classes) + '_norm.pkl.gz'

    print 'batch size', batch_size
    print 'iteration', nb_epoch
    print 'flag mode', train_flag
    print 'Reading the pickles from: ', pickle_adr
    print 'pickle_1: ', ad1
    print 'pickle_2: ', ad2
    print 'pickle_3: ', ad3
    print 'pickle_4: ', ad4
    print 'saving the trained model in: ', save_address

    datasets1 = my_load_dataset(ad1)
    test_set_x_1, test_set_y_1, test_set_name_1 = datasets1[2]
    valid_set_x_1, valid_set_y_1, valid_set_name_1 = datasets1[1]
    train_set_x_1, train_set_y_1, train_set_name_1 = datasets1[0]
    test_set_x_1 = test_set_x_1.reshape(-1, 1, img_rows, img_cols)
    train_set_x_1 = train_set_x_1.reshape(-1, 1, img_rows, img_cols)
    valid_set_x_1 = valid_set_x_1.reshape(-1, 1, img_rows, img_cols)

    datasets2 = my_load_dataset(ad2)
    test_set_x_2, test_set_y_2, test_set_name_2 = datasets2[2]
    valid_set_x_2, valid_set_y_2, valid_set_name_2 = datasets2[1]
    train_set_x_2, train_set_y_2, train_set_name_2 = datasets2[0]
    test_set_x_2 = test_set_x_2.reshape(-1, 1, img_rows, img_cols)
    train_set_x_2 = train_set_x_2.reshape(-1, 1, img_rows, img_cols)
    valid_set_x_2 = valid_set_x_2.reshape(-1, 1, img_rows, img_cols)

    datasets3 = my_load_dataset(ad3)
    test_set_x_3, test_set_y_3, test_set_name_3 = datasets3[2]
    valid_set_x_3, valid_set_y_3, valid_set_name_3 = datasets3[1]
    train_set_x_3, train_set_y_3, train_set_name_3 = datasets3[0]
    test_set_x_3 = test_set_x_3.reshape(-1, 1, img_rows, img_cols)
    train_set_x_3 = train_set_x_3.reshape(-1, 1, img_rows, img_cols)
    valid_set_x_3 = valid_set_x_3.reshape(-1, 1, img_rows, img_cols)

    datasets4 = my_load_dataset(ad4)
    test_set_x_4, test_set_y_4, test_set_name_4 = datasets4[2]
    valid_set_x_4, valid_set_y_4, valid_set_name_4 = datasets4[1]
    train_set_x_4, train_set_y_4, train_set_name_4 = datasets4[0]
    test_set_x_4 = test_set_x_4.reshape(-1, 1, img_rows, img_cols)
    train_set_x_4 = train_set_x_4.reshape(-1, 1, img_rows, img_cols)
    valid_set_x_4 = valid_set_x_4.reshape(-1, 1, img_rows, img_cols)

    assert (test_set_y_2 == test_set_y_1).all()
    assert (valid_set_y_2 == valid_set_y_1).all()
    assert (train_set_y_2 == train_set_y_1).all()

    assert (test_set_y_2 == test_set_y_3).all()
    assert (valid_set_y_2 == valid_set_y_3).all()
    assert (train_set_y_2 == train_set_y_3).all()

    assert (test_set_y_3 == test_set_y_4).all()
    assert (valid_set_y_3 == valid_set_y_4).all()
    assert (train_set_y_3 == train_set_y_4).all()

    assert (test_set_name_1 == test_set_name_2).all()
    assert (test_set_name_2 == test_set_name_3).all()
    assert (test_set_name_3 == test_set_name_4).all()

    assert (valid_set_name_1 == valid_set_name_2).all()
    assert (valid_set_name_2 == valid_set_name_3).all()
    assert (valid_set_name_3 == valid_set_name_4).all()

    assert (train_set_name_1 == train_set_name_2).all()
    assert (train_set_name_2 == train_set_name_3).all()
    assert (train_set_name_3 == train_set_name_4).all()

    cat_train_set_y_1 = np_utils.to_categorical(train_set_y_1, nb_classes)
    cat_valid_set_y_1 = np_utils.to_categorical(valid_set_y_1, nb_classes)
    cat_test_set_y_1 = np_utils.to_categorical(test_set_y_1, nb_classes)

    concat_train = square_early_concatenate_feature(train_set_x_1,
                                                    train_set_x_2,
                                                    train_set_x_3,
                                                    train_set_x_4,
                                                    [img_rows, img_cols])
    concat_test = square_early_concatenate_feature(test_set_x_1, test_set_x_2,
                                                   test_set_x_3, test_set_x_4,
                                                   [img_rows, img_cols])
    concat_valid = square_early_concatenate_feature(valid_set_x_1,
                                                    valid_set_x_2,
                                                    valid_set_x_3,
                                                    valid_set_x_4,
                                                    [img_rows, img_cols])

    cnn1 = build_cnn(img_rows * 2, img_cols * 2)
    final_model = Sequential()
    final_model.add(cnn1)
    final_model.add(Dense(nb_classes, activation='softmax'))

    #model_optimizer = RMSprop(lr=0.1)
    final_model.compile(
        loss='categorical_crossentropy',
        optimizer='adadelta',
        # optimizer=model_optimizer,
        metrics=['accuracy'])

    model_adr = save_address + '/models/'
    if not os.path.exists(model_adr):
        print('making models address ... ' + model_adr)
        os.makedirs(model_adr)

    acc_checker = ModelCheckpoint(model_adr + "/best_weights.h5",
                                  monitor='val_acc',
                                  verbose=1,
                                  save_best_only=True,
                                  mode='max',
                                  save_weights_only=True)

    if train_flag:
        print(concat_train.shape[0], 'train samples')
        print(concat_valid.shape[0], 'validation samples')
        print(concat_test.shape[0], 'test samples')

        final_model.fit(concat_train,
                        cat_train_set_y_1,
                        batch_size=batch_size,
                        epochs=nb_epoch,
                        verbose=1,
                        validation_data=(concat_valid, cat_valid_set_y_1),
                        callbacks=[acc_checker])

        final_model.load_weights(model_adr + "/best_weights.h5")
        score = final_model.evaluate(concat_test, cat_test_set_y_1, verbose=0)
        print('Test score:', score[0])
        print('Test accuracy:', score[1])

        #print final_model.summary()
        print 'done'
    else:
        all_data_for_train = np.append(concat_train, concat_valid, axis=0)
        all_data_for_train = np.append(all_data_for_train, concat_test, axis=0)
        all_label_for_train = np.append(cat_train_set_y_1,
                                        cat_valid_set_y_1,
                                        axis=0)
        all_label_for_train = np.append(all_label_for_train,
                                        cat_test_set_y_1,
                                        axis=0)

        print('Number of training samples:', all_data_for_train.shape[0])
        final_model.fit(all_data_for_train,
                        all_label_for_train,
                        batch_size=batch_size,
                        epochs=nb_epoch,
                        verbose=1,
                        validation_data=(concat_valid, cat_valid_set_y_1),
                        callbacks=[acc_checker])
        final_model.load_weights(model_adr + "/best_weights.h5")

    # save model and weights
    json_string = final_model.to_json()
    f = gzip.open(save_address + '/model.pklz', 'wb')
    cPickle.dump(json_string, f, protocol=cPickle.HIGHEST_PROTOCOL)
    f.close()
    final_model.save_weights(save_address + '/model_weights.h5',
                             overwrite=True)
コード例 #46
0
ファイル: ts_models.py プロジェクト: skiddles/LSTM_TensorFlow
        class Simple:
            def __init__(self):

                # Originally, input_shape_1 was train_X.shape[1]
                # and input_shape_2 was train_X.shape[2]
                self.random_state = None
                self.model = None
                self.train_X = None
                self.train_y = None
                self.test_X = None
                self.test_y = None
                self.predictions = None
                self.scores = None
                self.history = None

            def define_model(self,
                             input_shape_1,
                             input_shape_2,
                             random_state=61):
                """
                This initializes the Multivariate.LSTM.Simple model

                :param input_shape_1: Originally, train_X.shape[1], this is the length of the second dimension
                                      of the training dataset
                :param input_shape_2: Originally, train_X.shape[2], this is the length of the third dimension
                                      of the training dataset
                :param random_state: A parameter to set the random seed parameter
                """
                self.random_state = random_state
                self.model = Sequential()
                self.model.add(
                    LSTM(50, input_shape=(input_shape_1, input_shape_2)))
                self.model.add(Dense(1))
                self.model.compile(loss='mae', optimizer='adam')

            def set_train_data(self, train_data):
                self.train_X, self.train_y = train_data

                # self.scores = self.model.evaluate(X, Y, verbose=0)
                # print("%s: %.2f%%" % (self.model.metrics_names[1], self.scores[1] * 100))

            def set_test_data(self, test_data):
                self.test_X, self.test_y = test_data

            def train(self, epochs=150, batch_size=50, verbose=0):
                assert self.train_X is not None
                assert self.train_y is not None
                self.history = self.model.fit(self.train_X,
                                              self.train_y,
                                              epochs=epochs,
                                              batch_size=batch_size,
                                              validation_data=(self.test_X,
                                                               self.test_y),
                                              verbose=verbose,
                                              shuffle=False)

            def predict(self):
                self.predictions = self.model.predict(self.test_X)
                print(self.predictions[-5:, :])

            def save(self, filename):
                filenm, extn = os.path.splitext(filename)
                model_json = self.model.to_json()
                with open(filename, "w") as json_file:
                    json_file.write(model_json)
                # serialize weights to HDF5
                wt_filename = '.'.join([filenm, "h5"])
                print("Saving weights and biases to '%s'" % wt_filename)
                self.model.save_weights(wt_filename)
                print("Saved model to disk")

            def load(self, filename):
                filenm, extn = os.path.splitext(filename)
                json_file = open(filename, 'r')
                loaded_model_json = json_file.read()
                json_file.close()
                self.model = model_from_json(loaded_model_json)
                # load weights into new model
                wt_filename = '.'.join([filenm, "h5"])
                print("Loading weights and biases from '%s'" % wt_filename)
                self.model.load_weights(wt_filename)
                print("Loaded model from disk")

            def evaluate_model(self):
                # evaluate loaded model on test data
                self.model.compile(loss='mae', optimizer='adam')
                self.scores = self.model.evaluate(self.test_X,
                                                  self.test_y,
                                                  verbose=2)
                print(self.model.metrics_names)
コード例 #47
0
class WordVecCnnLstm(object):
    model_name = 'wordvec_cnn_lstm'

    def __init__(self):
        self.model = None
        self.word2idx = None
        self.idx2word = None
        self.max_len = None
        self.config = None
        self.vocab_size = None
        self.labels = None

    @staticmethod
    def get_architecture_file_path(model_dir_path):
        return model_dir_path + '/' + WordVecCnnLstm.model_name + '_architecture.json'

    @staticmethod
    def get_weight_file_path(model_dir_path):
        return model_dir_path + '/' + WordVecCnnLstm.model_name + '_weights.h5'

    @staticmethod
    def get_config_file_path(model_dir_path):
        return model_dir_path + '/' + WordVecCnnLstm.model_name + '_config.npy'

    def load_model(self, model_dir_path):
        json = open(self.get_architecture_file_path(model_dir_path),
                    'r').read()
        self.model = model_from_json(json)
        self.model.load_weights(self.get_weight_file_path(model_dir_path))
        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])

        config_file_path = self.get_config_file_path(model_dir_path)

        self.config = np.load(config_file_path).item()

        self.idx2word = self.config['idx2word']
        self.word2idx = self.config['word2idx']
        self.max_len = self.config['max_len']
        self.vocab_size = self.config['vocab_size']
        self.labels = self.config['labels']

    def create_model(self):
        lstm_output_size = 70
        embedding_size = 100
        self.model = Sequential()
        self.model.add(
            Embedding(input_dim=self.vocab_size,
                      input_length=self.max_len,
                      output_dim=embedding_size))
        self.model.add(SpatialDropout1D(0.2))
        self.model.add(
            Conv1D(filters=256,
                   kernel_size=5,
                   padding='same',
                   activation='relu'))
        self.model.add(MaxPooling1D(pool_size=4))
        self.model.add(LSTM(lstm_output_size))
        self.model.add(Dense(units=len(self.labels), activation='softmax'))

        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])

    def fit(self,
            text_data_model,
            text_label_pairs,
            model_dir_path,
            batch_size=None,
            epochs=None,
            test_size=None,
            random_state=None):
        if batch_size is None:
            batch_size = 64
        if epochs is None:
            epochs = 20
        if test_size is None:
            test_size = 0.3
        if random_state is None:
            random_state = 42

        self.config = text_data_model
        self.idx2word = self.config['idx2word']
        self.word2idx = self.config['word2idx']
        self.max_len = self.config['max_len']
        self.vocab_size = self.config['vocab_size']
        self.labels = self.config['labels']

        np.save(self.get_config_file_path(model_dir_path), self.config)

        self.create_model()
        json = self.model.to_json()
        open(self.get_architecture_file_path(model_dir_path), 'w').write(json)

        xs = []
        ys = []
        for text, label in text_label_pairs:
            tokens = [x.lower() for x in word_tokenize(text)]
            wid_list = list()
            for w in tokens:
                wid = 0
                if w in self.word2idx:
                    wid = self.word2idx[w]
                wid_list.append(wid)
            xs.append(wid_list)
            ys.append(self.labels[label])

        X = pad_sequences(xs, maxlen=self.max_len)
        Y = np_utils.to_categorical(ys, len(self.labels))

        x_train, x_test, y_train, y_test = train_test_split(
            X, Y, test_size=test_size, random_state=random_state)
        print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

        weight_file_path = self.get_weight_file_path(model_dir_path)

        checkpoint = ModelCheckpoint(weight_file_path)

        history = self.model.fit(x=x_train,
                                 y=y_train,
                                 batch_size=batch_size,
                                 epochs=epochs,
                                 validation_data=[x_test, y_test],
                                 callbacks=[checkpoint],
                                 verbose=1)

        self.model.save_weights(weight_file_path)

        np.save(
            model_dir_path + '/' + WordVecCnnLstm.model_name + '-history.npy',
            history.history)

        score = self.model.evaluate(x=x_test,
                                    y=y_test,
                                    batch_size=batch_size,
                                    verbose=1)
        print('score: ', score[0])
        print('accuracy: ', score[1])

        return history

    def predict(self, sentence):
        xs = []
        tokens = [w.lower() for w in word_tokenize(sentence)]
        wid = [
            self.word2idx[token]
            if token in self.word2idx else len(self.word2idx)
            for token in tokens
        ]
        xs.append(wid)
        x = pad_sequences(xs, self.max_len)
        output = self.model.predict(x)
        return output[0]

    def predict_class(self, sentence):
        predicted = self.predict(sentence)
        idx2label = dict([(idx, label) for label, idx in self.labels.items()])
        return idx2label[np.argmax(predicted)]

    def test_run(self, sentence):
        print(self.predict(sentence))

    def export_tensorflow_model(self, output_fld):
        export_keras_to_tensorflow(
            self.model,
            output_fld,
            output_model_file=WordVecCnnLstm.model_name + '.pb')
        export_text_model_to_csv(self.config,
                                 output_fld,
                                 output_model_file=WordVecCnnLstm.model_name +
                                 '.csv')
コード例 #48
0
test_set = test_datagen.flow_from_directory('dataset/testing',
                                            target_size=(48, 48),
                                            batch_size=16,
                                            class_mode='binary',
                                            color_mode='grayscale')

classifier.fit_generator(train_set,
                         steps_per_epoch=8575,
                         epochs=5,
                         validation_data=test_set,
                         validation_steps=2000)
print('done!!')

classifier.save('cdi.h5')
classifier.save_weights('cdiw.h5')
jstring = classifier.to_json()
jfile = open('classifier.json', 'w')
jfile.write(jstring)
jfile.close()

# from keras.models import load_model

# classifier = load_model('cdi.h5')

# import numpy as np
# from keras.preprocessing import image
# import cv2
# import os

# img = cv2.imread('spred\\9.jpg',0)
# img2 =cv2.imread('spred\\8.jpg')
def model9(X_train, y_train,X_test, y_test,in_batch_size=100,in_epochs=10,model_json_file="model.json",model_h5_file="model.h5"): # RNN: Recurrent Neural Networks
    # https://keras.io/getting-started/sequential-model-guide/

    # Convert labels to categorical one-hot encoding  
    y_train_one_hot_labels = to_categorical(y_train, num_classes=n_classes)
    y_test_one_hot_labels = to_categorical(y_test, num_classes=n_classes)

    # create the model
    embedding_vecor_length = 4
    model = Sequential()
    model.add(Embedding(num_letters, embedding_vecor_length, input_length=fixed_seq_length))
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, padding='same', activation='relu')))
    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(MaxPooling1D(3)))
    model.add(TimeDistributed(Conv1D(filters=128, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Conv1D(filters=128, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(MaxPooling1D(3)))
    model.add(TimeDistributed(Conv1D(filters=256, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Conv1D(filters=256, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(MaxPooling1D(3)))
    model.add(TimeDistributed(Conv1D(filters=512, kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Conv1D(filters=512, kernel_size=3, activation='relu')))
    #model.add(GlobalAveragePooling1D())
    model.add(TimeDistributed(Dropout(0.25)))
    model.add(TimeDistributed(Dense(4096, activation='relu')))
    model.add(TimeDistributed(Dropout(0.25)))
    model.add(TimeDistributed(Dense(4096, activation='relu')))
    model.add(TimeDistributed(Dropout(0.25)))
    
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    #model.add(GRU(128, return_sequences=True))
    model.add(LSTM(128,return_sequences=True))
    model.add(LSTM(256,return_sequences=False))
        
    model.add(Dropout(0.5))
    model.add(Dense(n_classes, activation='softmax'))
    
    #opt = 'rmsprop'
    #opt = optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
    #opt = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=0.001, decay=0.0)
    #opt = optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
    #opt = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=None, decay=0.0)
    opt = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
    #opt = optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
    #opt = optimizers.Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, schedule_decay=0.004)

    model.compile(loss='categorical_crossentropy',optimizer=opt, metrics=['accuracy'])
    print(model.summary())

    # fit the model
    history = model.fit(X_train, y_train_one_hot_labels, epochs=in_epochs, batch_size=in_batch_size,validation_data=(X_test, y_test_one_hot_labels), verbose=1)

    # evaluate the model
    train_loss, train_acc = model.evaluate(X_train, y_train_one_hot_labels, verbose=0)
    test_loss, test_acc = model.evaluate(X_test, y_test_one_hot_labels, verbose=0)
    print('Loss     ======= Train: %.3f, Test: %.3f' % (train_loss, test_loss))
    print('Accuracy ======= Train: %.3f, Test: %.3f' % (train_acc, test_acc))

    # summarize history for accuracy
    pyplot.plot(history.history['acc'])
    pyplot.plot(history.history['val_acc'])
    pyplot.title('model accuracy')
    pyplot.ylabel('accuracy')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'test'], loc='upper left')
    #pyplot.show()
    pyplot.savefig('module9_accuracy.png')
    # summarize history for loss
    pyplot.plot(history.history['loss'])
    pyplot.plot(history.history['val_loss'])
    pyplot.title('model loss')
    pyplot.ylabel('loss')
    pyplot.xlabel('epoch')
    pyplot.legend(['train', 'test'], loc='upper left')
    #pyplot.show()
    pyplot.savefig('module9_loss.png')

    # serialize model to JSON
    model_json = model.to_json()
    with open(model_json_file, "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(model_h5_file)
    print("Saved model to disk")
    
    return()
コード例 #50
0
def fit_lstm(train,
             batch_size,
             nb_epoch,
             neurons,
             time_steps=1,
             model_file_name="model",
             validation_data=None):
    """
This function assembles your network and train it with provided data (train).
It returns your fitted model.

    :param train: Training data in shape (samples x features)
    :param batch_size: Size of each batch. For ONLINE training, batch == 1.
    :param nb_epoch: How many epochs to train.
    :param neurons: How many neurons in the LSTM cell.
    :param time_steps: For a stateful LSTM, usually we have 1 time step and it's hard to think in an alternative case, so we leave it optional.
    :param model_file_name: File name to save model after training.
    :param validation_data: Same pattern that training data, but the portion of validation to check overfitting, etc.
    :return: Your trained model.
    """
    X, y = train[:, 0:-1], train[:, -1]

    X_validation, y_validation = validation_data[:, 0:-1], validation_data[:,
                                                                           -1]

    # (Samples, Time STEPS, Fetures)
    # Ex: 266 amostras com 1 time step (NAO tem a ver com online training) e 1 feature (1 entrada)
    X = X.reshape(X.shape[0], time_steps, X.shape[1])

    X_validation = X_validation.reshape(X_validation.shape[0], time_steps,
                                        X_validation.shape[1])
    model = Sequential()
    # A quantidade de amostras (ex: 266) nao eh levada em conta aqui, o que
    # importa eh o shape de entrada, por isso nao usa X.shape[0].
    # O batch_size indica quantas SAMPLES sao usadas para avaliar o gradiente
    # DE UMA VEZ. No caso, ajustamos a rede a cada 1 sample (onilne training),
    # que eh o tamanho do batch
    model.add(
        LSTM(neurons,
             batch_input_shape=(batch_size, X.shape[1], X.shape[2]),
             stateful=True))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='nadam')

    keras_callback = TensorBoard(log_dir="./logs",
                                 histogram_freq=1,
                                 batch_size=batch_size,
                                 write_grads=True,
                                 write_graph=True,
                                 write_images=True,
                                 update_freq="epoch",
                                 embeddings_freq=0,
                                 embeddings_metadata=None)

    for i in tqdm(range(1)):
        # I believe we need to train each epoch and then reset states, beacause
        # this is a stateful lstm, and it would "remember" previous inputs if we
        # didn't reset it.
        training_history = model.fit(X,
                                     y,
                                     epochs=nb_epoch,
                                     batch_size=batch_size,
                                     verbose=1,
                                     shuffle=False,
                                     validation_data=(X_validation,
                                                      y_validation),
                                     callbacks=[keras_callback])
        model.reset_states()

    # serialize model to JSON
    model_json = model.to_json()
    with open(model_file_name + ".json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(model_file_name + ".h5")
    return model, training_history
コード例 #51
0
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))
#####

adam = Adam(lr=0.001)
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()
validation_split = 0.10
model.fit(X,
          y,
          batch_size=128,
          class_weight=class_weight,
          nb_epoch=70,
          verbose=1,
          validation_split=validation_split)
open('model.json', 'w').write(model.to_json())
model.save_weights('weights.h5')

plt.plot(model.model.history.history['loss'])
#plt.plot(model.model.history.history['acc'])
#plt.plot(model.model.history.history['val_loss'])
plt.plot(model.model.history.history['val_acc'])
plt.show()

n_validation = int(len(X) * validation_split)
y_predicted = model.predict(X[-n_validation:])
print(roc_auc_score(y[-n_validation:], y_predicted))
コード例 #52
0
model.add(Conv2D(64, (3, 3), padding='same', activation="relu"))
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(512, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation="softmax"))

# Compile the model
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# Train the model
model.fit(x_train,
          y_train,
          batch_size=64,
          epochs=30,
          validation_data=(x_test, y_test),
          shuffle=True)

# Save neural network structure
model_structure = model.to_json()
f = Path("model_structure.json")
f.write_text(model_structure)

# Save neural network's trained weights
model.save_weights("model_weights.h5")
コード例 #53
0
            if len(exp_replay_O.memory) > 0:
                inputs, targets = exp_replay_O.get_batch(model1,
                                                         model2,
                                                         batch_size=batch_size)
                if len(targets) > 0 and np.amax(targets) > 0:
                    loss_O += model1.train_on_batch(inputs, targets)[0]
                exp_replay_O.memory = list()

            if len(exp_replay_X.memory) > 0:
                inputs, targets = exp_replay_X.get_batch(model2,
                                                         model1,
                                                         batch_size=batch_size)
                if len(targets) > 0 and np.amax(targets) > 0:
                    loss_X += model2.train_on_batch(inputs, targets)[0]
                exp_replay_X.memory = list()

        if e % display_epoch == display_epoch - 1:
            print("Learnings : " + str(learnings))
            print(
                "Epoch " + str(e) + " / " + str(stop_epoch) +
                "| Loss_X {:.4f} | Loss_O {:.4f} | X Win count {}| O Win count {} | Draws {}"
                .format(loss_X, loss_O, x_win_cnt, o_win_cnt, draws))
            print("Time : " + str(time.time() - start))
            model1.save_weights("modelO.h5", overwrite=True)
            model2.save_weights("modelX.h5", overwrite=True)
            with open("modelO_full.json", "w") as outfile:
                json.dump(model1.to_json(), outfile)
            with open("modelX_full.json", "w") as outfile:
                json.dump(model2.to_json(), outfile)
コード例 #54
0
ファイル: SmallCNN.py プロジェクト: KeyboardsEater/FER
def CNNSmall():

    # dimensions of our images.
    img_width, img_height = 150, 150

    train_data_dir = 'dataFromPython2/train'
    validation_data_dir = 'dataFromPython2/validation'
    nb_train_samples = 2290  #168 #2290 #168 #2318 # 506 #2318 #362
    nb_validation_samples = 770  #41# 770#41 #=771 # 124# 771 #246
    epochs = 50
    batch_size = 10  # 100 # 16

    if K.image_data_format() == 'channels_first':
        input_shape = (3, img_width, img_height)
    else:
        input_shape = (img_width, img_height, 3)

    model = Sequential()
    model.add(Conv2D(32, (3, 3), input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(32, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(124, (3, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    #model.add(Dropout(0.5))
    model.add(Dropout(0.5))
    model.add(Dense(7))
    #model.add(Activation('sigmoid'))
    model.add(Activation('softmax'))
    '''model.compile(loss='binary_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])'''
    '''model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])'''

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    # this is the augmentation configuration we will use for training
    train_datagen = ImageDataGenerator(rescale=1. / 255,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)

    # this is the augmentation configuration we will use for testing:
    # only rescaling
    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical')  # binary

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical')  #binary

    model.fit_generator(train_generator,
                        steps_per_epoch=nb_train_samples // batch_size,
                        epochs=epochs,
                        validation_data=validation_generator,
                        validation_steps=nb_validation_samples // batch_size)

    #model.save_weights('first_try.h5')

    # serialize model to JSON
    dir = dir = '//Users/HP/Desktop/dissertation/code/Esmam2017-mscprojem-6c2a81260da0'  # '//Users/emb24/PycharmProjects/EmotionFacialUnitsVideo/'
    model_json = model.to_json()
    with open("SmallCNNADAM1.json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights("SMallCNNWeightADAM1.h5")
    print("Saved model to disk")
コード例 #55
0
ファイル: clothes_up_low_whole.py プロジェクト: mahomeL/QM_dl
def get_model_ori(save_name=get_model_ori_weights_save_name,
                  save_weights_flag=save_weights_flag,
                  batch_size=32,
                  art_name=get_model_ori_art_save_name):

    model = Sequential()
    model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Convolution2D(32, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Convolution2D(64, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(2 * dense_last2))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2 * dense_last2))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))

    model.compile(
        loss='categorical_crossentropy',
        optimizer='rmsprop',  #rmsprop
        metrics=['accuracy'])

    train_datagen = ImageDataGenerator(
        rescale=1. / 255,
        # samplewise_center=True,
        shear_range=0.,
        rotation_range=0.2,
        fill_mode='nearest',
        vertical_flip=True,
        horizontal_flip=True,
    )

    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical')

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical')

    early_stopping = EarlyStopping(monitor='val_acc', patience=3, verbose=1)
    save_best = ModelCheckpoint(save_name,
                                verbose=1,
                                monitor='val_acc',
                                save_best_only=True,
                                save_weights_only=save_weights_flag)
    print('model input_shape:{}'.format(model.input_shape))
    # model.fit_generator(
    #         train_generator,
    #         samples_per_epoch=nb_train_samples,
    #         nb_epoch=nb_epoch + 10,
    #         validation_data=validation_generator,
    #         nb_val_samples=nb_validation_samples,
    #         verbose=1,
    #         callbacks=[save_best,early_stopping])#

    from keras.models import model_from_json
    # save
    if get_model_ori_art_save_name:
        json_str = model.to_json()
        with open(get_model_ori_art_save_name, 'w') as f:
            f.write(json_str)

    return model
コード例 #56
0
def get_all_by_name(name):
    import numpy as np
    if os.path.exists(util.features_prefix + name + "_XY.pkl") is False:
        print name + 'file does not exist'
        exit()
    if os.path.exists(util.features_prefix + name + '_XXXYYY.pkl') is False:
        [train_X,
         train_Y] = pd.read_pickle(util.features_prefix + name + '_XY.pkl')
        X_train, X_test, y_train, y_test = cross_validation.train_test_split(
            train_X, train_Y, test_size=0.33, random_state=0)
        X_train, X_validate, y_train, y_validate = cross_validation.train_test_split(
            X_train, y_train, test_size=0.33, random_state=0)
        X_train = np.array(X_train)
        y_train = np.array(y_train)
        y_test = np.array(y_test)
        X_test = np.array(X_test)
        X_validate = np.array(X_validate)
        y_validate = np.array(y_validate)
        pd.to_pickle(
            [X_train, X_validate, X_test, y_train, y_validate, y_test],
            util.features_prefix + name + '_XXXYYY.pkl')
    if os.path.exists(util.features_prefix + name + '_XXXYYY.pkl'):
        from sklearn.ensemble import RandomForestClassifier

        if rf_test is False:
            [train_X,
             train_Y] = pd.read_pickle(util.features_prefix + name + '_XY.pkl')
            [X_train, X_validate, X_test, y_train, y_validate, y_test
             ] = pd.read_pickle(util.features_prefix + name + '_XXXYYY.pkl')
            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            print 'rf'
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            # for max in range(12, 23, 5):
            clf = RandomForestClassifier(n_jobs=4,
                                         n_estimators=400,
                                         max_depth=22)
            clf.fit(x, y)
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            pd.to_pickle(clf, util.models_prefix + name + '_rf.pkl')
            y_p = clf.predict(X_test)
            print name + ' score:' + util.score_lists(y_test, y_p)

        if xgb_test is False:
            [train_X,
             train_Y] = pd.read_pickle(util.features_prefix + name + '_XY.pkl')
            [X_train, X_validate, X_test, y_train, y_validate, y_test
             ] = pd.read_pickle(util.features_prefix + name + '_XXXYYY.pkl')
            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            print 'xg'
            import xgboost as xgb

            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            set_y = set(train_Y)
            param["num_class"] = len(set_y)

            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            dtrain = xgb.DMatrix(x, label=y)
            param['objective'] = 'multi:softmax'
            xgb_2 = xgb.train(param, dtrain, keys[name][0])
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            xgb_2.save_model(util.models_prefix + name + '_xgb.pkl')
            dtest = xgb.DMatrix(X_test)
            y_p = xgb_2.predict(dtest)
            print name + ' score:' + util.score_lists(y_test, y_p)
            param['objective'] = 'multi:softprob'
            dtrain = xgb.DMatrix(x, label=y)
            xgb_1 = xgb.train(param, dtrain, keys[name][0])
            xgb_1.save_model(util.models_prefix + name + '_xgb_prob.pkl')

        if cnn_test is False:
            [train_X,
             train_Y] = pd.read_pickle(util.features_prefix + name + '_XY.pkl')
            [X_train, X_validate, X_test, y_train, y_validate, y_test
             ] = pd.read_pickle(util.features_prefix + name + '_XXXYYY.pkl')
            print 'cnn'
            import copy
            import numpy as np
            from sklearn.preprocessing import LabelEncoder
            from keras.utils import np_utils
            from keras.layers.convolutional import Convolution1D
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            label_dict = LabelEncoder().fit(train_Y)
            label_num = len(label_dict.classes_)
            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            train_Y = np_utils.to_categorical(y, label_num)
            # x = np.concatenate([X_train, X_validate], axis=0)
            X_train = x
            X_semantic = np.array(copy.deepcopy(X_train[:, range(95, 475)]))
            X_manual = np.array(copy.deepcopy(X_train[:, range(0, 95)]))
            X_cluster = np.array(copy.deepcopy(X_train[:, range(475, 545)]))
            X_document = np.array(copy.deepcopy(X_train[:, range(545, 547)]))
            X_document[:, [0]] = X_document[:, [0]] + train_X[:, [-1]].max()
            dic_num_cluster = X_cluster.max()
            dic_num_manual = train_X.max()
            dic_num_document = X_document[:, [0]].max()
            from keras.models import Sequential
            from keras.layers.embeddings import Embedding
            from keras.layers.core import Merge
            from keras.layers.core import Dense, Dropout, Activation, Flatten
            from keras.layers.recurrent import LSTM

            X_semantic = X_semantic.reshape(X_semantic.shape[0], 10, -1)
            X_semantic_1 = np.zeros((X_semantic.shape[0], X_semantic.shape[2],
                                     X_semantic.shape[1]))
            for i in range(int(X_semantic.shape[0])):
                X_semantic_1[i] = np.transpose(X_semantic[i])
            model_semantic = Sequential()
            model_lstm = Sequential()
            model_lstm.add(
                LSTM(output_dim=30,
                     input_shape=X_semantic_1.shape[1:],
                     go_backwards=True))
            model_semantic.add(
                Convolution1D(nb_filter=32,
                              filter_length=2,
                              border_mode='valid',
                              activation='relu',
                              input_shape=X_semantic_1.shape[1:]))
            # model_semantic.add(MaxPooling1D(pool_length=2))
            model_semantic.add(
                Convolution1D(nb_filter=8,
                              filter_length=2,
                              border_mode='valid',
                              activation='relu'))
            # model_semantic.add(MaxPooling1D(pool_length=2))
            model_semantic.add(Flatten())

            # we use standard max pooling (halving the output of the previous layer):
            model_manual = Sequential()
            model_manual.add(
                Embedding(input_dim=dic_num_manual + 1,
                          output_dim=20,
                          input_length=X_manual.shape[1]))
            # model_manual.add(Convolution1D(nb_filter=2,
            #                                filter_length=2,
            #                                border_mode='valid',
            #                                activation='relu'))
            # model_manual.add(MaxPooling1D(pool_length=2))
            # model_manual.add(Convolution1D(nb_filter=8,
            #                                filter_length=2,
            #                                border_mode='valid',
            #                                activation='relu'))
            # model_manual.add(MaxPooling1D(pool_length=2))
            model_manual.add(Flatten())

            model_document = Sequential()
            model_document.add(
                Embedding(input_dim=dic_num_document + 1,
                          output_dim=2,
                          input_length=X_document.shape[1]))
            model_document.add(Flatten())

            model_cluster = Sequential()
            model_cluster.add(
                Embedding(input_dim=dic_num_cluster + 1,
                          output_dim=5,
                          input_length=X_cluster.shape[1]))
            model_cluster.add(Flatten())
            model = Sequential()
            # model = model_cluster
            model.add(
                Merge([
                    model_document, model_cluster, model_manual, model_semantic
                ],
                      mode='concat',
                      concat_axis=1))
            model.add(Dense(512))
            model.add(Dropout(0.5))
            model.add(Activation('relu'))
            model.add(Dense(128))
            model.add(Dropout(0.5))
            model.add(Activation('relu'))
            # We project onto a single unit output layer, and squash it with a sigmoid:
            model.add(Dense(label_num))
            model.add(Activation('softmax'))

            model.compile(loss='categorical_crossentropy',
                          optimizer='adadelta')
            # model.fit(X_cluster_1, train_Y, batch_size=100,
            #           nb_epoch=100, validation_split=0.33, verbose=1)
            model.fit([X_document, X_cluster, X_manual, X_semantic_1],
                      train_Y,
                      batch_size=100,
                      nb_epoch=keys[name][1])
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            json_string = model.to_json()
            pd.to_pickle(json_string,
                         util.models_prefix + name + '_json_string_cnn.pkl')
            model.save_weights(util.models_prefix + name + '_nn_weight_cnn.h5')
            X_semantic = np.array(copy.deepcopy(X_test[:, range(95, 475)]))
            X_manual = np.array(copy.deepcopy(X_test[:, range(0, 95)]))
            X_cluster = np.array(copy.deepcopy(X_test[:, range(475, 545)]))
            X_document = np.array(copy.deepcopy(X_test[:, range(545, 547)]))
            X_document[:, [0]] = X_document[:, [0]] + train_X[:, [-1]].max()
            X_semantic = X_semantic.reshape(X_semantic.shape[0], 10, -1)
            X_semantic_1 = np.zeros((X_semantic.shape[0], X_semantic.shape[2],
                                     X_semantic.shape[1]))
            for i in range(int(X_semantic.shape[0])):
                X_semantic_1[i] = np.transpose(X_semantic[i])
            cnn_list = model.predict_classes(
                [X_document, X_cluster, X_manual, X_semantic_1])
            print name + ' score:' + util.score_lists(y_test, cnn_list)

        if lstm_test is False:
            import numpy as np

            [train_X,
             train_Y] = pd.read_pickle(util.features_prefix + name + '_XY.pkl')
            [X_train, X_validate, X_test, y_train, y_validate, y_test
             ] = pd.read_pickle(util.features_prefix + name + '_XXXYYY.pkl')
            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            print 'lstm'
            import copy
            import numpy as np
            from sklearn.preprocessing import LabelEncoder
            from keras.utils import np_utils
            from keras.layers.convolutional import Convolution1D

            label_dict = LabelEncoder().fit(train_Y)
            label_num = len(label_dict.classes_)
            x = np.concatenate([X_train, X_validate], axis=0)
            y = np.concatenate([y_train, y_validate], axis=0)
            train_Y = np_utils.to_categorical(y, label_num)
            # x = np.concatenate([X_train, X_validate], axis=0)
            X_train = x
            X_semantic = np.array(copy.deepcopy(X_train[:, range(95, 475)]))
            X_manual = np.array(copy.deepcopy(X_train[:, range(0, 95)]))
            X_cluster = np.array(copy.deepcopy(X_train[:, range(475, 545)]))
            X_document = np.array(copy.deepcopy(X_train[:, range(545, 547)]))
            X_document[:, [0]] = X_document[:, [0]] + train_X[:, [-1]].max()
            dic_num_cluster = X_cluster.max()
            dic_num_manual = train_X.max()
            dic_num_document = X_document[:, [0]].max()
            from keras.models import Sequential
            from keras.layers.embeddings import Embedding
            from keras.layers.core import Merge
            from keras.layers.core import Dense, Dropout, Activation, Flatten
            from keras.layers.recurrent import LSTM

            X_semantic = X_semantic.reshape(X_semantic.shape[0], 10, -1)
            X_semantic_1 = np.zeros((X_semantic.shape[0], X_semantic.shape[2],
                                     X_semantic.shape[1]))
            for i in range(int(X_semantic.shape[0])):
                X_semantic_1[i] = np.transpose(X_semantic[i])
            model_semantic = Sequential()
            model_lstm = Sequential()
            model_lstm.add(
                LSTM(output_dim=30,
                     input_shape=X_semantic_1.shape[1:],
                     go_backwards=True))
            model_semantic.add(
                Convolution1D(nb_filter=32,
                              filter_length=2,
                              border_mode='valid',
                              activation='relu',
                              input_shape=X_semantic_1.shape[1:]))
            # model_semantic.add(MaxPooling1D(pool_length=2))
            model_semantic.add(
                Convolution1D(nb_filter=8,
                              filter_length=2,
                              border_mode='valid',
                              activation='relu'))
            # model_semantic.add(MaxPooling1D(pool_length=2))
            model_semantic.add(Flatten())

            # we use standard max pooling (halving the output of the previous layer):
            model_manual = Sequential()
            model_manual.add(
                Embedding(input_dim=dic_num_manual + 1,
                          output_dim=20,
                          input_length=X_manual.shape[1]))
            # model_manual.add(Convolution1D(nb_filter=2,
            #                                filter_length=2,
            #                                border_mode='valid',
            #                                activation='relu'))
            # model_manual.add(MaxPooling1D(pool_length=2))
            # model_manual.add(Convolution1D(nb_filter=8,
            #                                filter_length=2,
            #                                border_mode='valid',
            #                                activation='relu'))
            # model_manual.add(MaxPooling1D(pool_length=2))
            model_manual.add(Flatten())

            model_document = Sequential()
            model_document.add(
                Embedding(input_dim=dic_num_document + 1,
                          output_dim=2,
                          input_length=X_document.shape[1]))
            model_document.add(Flatten())

            model_cluster = Sequential()
            model_cluster.add(
                Embedding(input_dim=dic_num_cluster + 1,
                          output_dim=5,
                          input_length=X_cluster.shape[1]))
            model_cluster.add(Flatten())
            model = Sequential()
            # model = model_cluster
            model.add(
                Merge(
                    [model_document, model_cluster, model_manual, model_lstm],
                    mode='concat',
                    concat_axis=1))
            model.add(Dense(512))
            model.add(Dropout(0.5))
            model.add(Activation('relu'))
            model.add(Dense(128))
            model.add(Dropout(0.5))
            model.add(Activation('relu'))
            # We project onto a single unit output layer, and squash it with a sigmoid:
            model.add(Dense(label_num))
            model.add(Activation('softmax'))

            model.compile(loss='categorical_crossentropy',
                          optimizer='adadelta')
            # model.fit(X_cluster_1, train_Y, batch_size=100,
            #           nb_epoch=100, validation_split=0.33, verbose=1)
            model.fit([X_document, X_cluster, X_manual, X_semantic_1],
                      train_Y,
                      batch_size=100,
                      nb_epoch=keys[name][2])
            print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            json_string = model.to_json()
            pd.to_pickle(json_string,
                         util.models_prefix + name + '_json_string_lstm.pkl')
            model.save_weights(util.models_prefix + name +
                               '_nn_weight_lstm.h5')
            X_semantic = np.array(copy.deepcopy(X_test[:, range(95, 475)]))
            X_manual = np.array(copy.deepcopy(X_test[:, range(0, 95)]))
            X_cluster = np.array(copy.deepcopy(X_test[:, range(475, 545)]))
            X_document = np.array(copy.deepcopy(X_test[:, range(545, 547)]))
            X_document[:, [0]] = X_document[:, [0]] + train_X[:, [-1]].max()
            X_semantic = X_semantic.reshape(X_semantic.shape[0], 10, -1)
            X_semantic_1 = np.zeros((X_semantic.shape[0], X_semantic.shape[2],
                                     X_semantic.shape[1]))
            for i in range(int(X_semantic.shape[0])):
                X_semantic_1[i] = np.transpose(X_semantic[i])
            lstm_list = model.predict_classes(
                [X_document, X_cluster, X_manual, X_semantic_1])
            print name + ' score:' + util.score_lists(y_test, lstm_list)
コード例 #57
0
           input_shape=(1, 24, 24)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(24, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(16, activation='softmax'))

# compile the model
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# train it
model.fit(X_train, Y_train, batch_size=32, epochs=20, verbose=1)

# evaluate it
print()
print(model.evaluate(X_test, Y_test, verbose=0))
print()

# save it
model.save_weights('models/futhark_cnn_model.h5')
with open('models/futhark_cnn_model.json', 'w') as file:
    file.write(model.to_json())

print(
    "Saved model to 'models/futhark_cnn_model.json' and 'models/futhark_cnn_model.h5'"
)
print()
コード例 #58
0
                                                 class_mode='binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size=(64, 64),
                                            batch_size=32,
                                            class_mode='binary')

classifier.fit_generator(training_set,
                         steps_per_epoch=8000,
                         epochs=25,
                         validation_data=test_set,
                         validation_steps=2000)

# OPTIONAL: Save the model for future uses
# serialize model to JSON
model_json = classifier.to_json()
with open("cnn_model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
classifier.save_weights("cnn_model.h5")
print("Saved model to disk")

# OPTIONAL: Loading the saved model from disk
#json_file = open('cnn_model.json', 'r')
#loaded_model_json = json_file.read()
# json_file.close()
#new_classifier = model_from_json(loaded_model_json)
# load weights into new model
# new_classifier.load_weights('cnn_model.h5')
#print("Loaded model from disk")
# Compiling the new model for it's previous use
コード例 #59
0
# Обучение
history = model.fit(x_train, y_train,
                    batch_size=BATCH_SIZE,
                    epochs=NB_EPOCH,
                    validation_split=VALIDATION_SPLIT,
                    verbose=VERBOSE)

# Тестирование
score = model.evaluate(x_test, y_test,
                       batch_size=BATCH_SIZE,
                       verbose=VERBOSE)

print('Test accuracy: ', score[1])

# Сохранение модели
model_json = model.to_json()
open('cifar10_CNN.json', 'w').write(model_json)

# Сохранение весов
model.save_weights('cifar10_weights_CNN.h5', overwrite=True)

# График точности
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# График потери
コード例 #60
0
tb_cb = keras.callbacks.TensorBoard('./logs/', histogram_freq=1)
cbks = [tb_cb]
###

# モデルのサマリを表示
# model.summary()
plot_model(model, to_file='./model2.png')

history = model.fit(X_train,
                    Y_train,
                    batch_size=batch_size,
                    nb_epoch=nb_epoch,
                    verbose=1,
                    validation_split=0.1,
                    callbacks=cbks)

# 学習履歴をプロット
TensorBoard(log_dir='./logs')

### add for TensorBoard

KTF.set_session(old_session)

###

## 学習済みモデルの保存
json_string = model.to_json()
open(os.path.join('./', 'cnn_model.json'), 'w').write(json_string)

model.save_weights(os.path.join('./', 'cnn_model_weight.hdf5'))