当前位置: 首页>>代码示例>>Python>>正文


Python Sequential.to_json方法代码示例

本文整理汇总了Python中keras.models.Sequential.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Sequential.to_json方法的具体用法?Python Sequential.to_json怎么用?Python Sequential.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在keras.models.Sequential的用法示例。


在下文中一共展示了Sequential.to_json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_nested_sequential

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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)
开发者ID:5ke,项目名称:keras,代码行数:62,代码来源:test_sequential_model.py

示例2: move_grade_training

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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())
开发者ID:jjrob13,项目名称:deep_ultimate_tic_tac_toe,代码行数:62,代码来源:train.py

示例3: test_nested_sequential

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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)
开发者ID:CheRaissi,项目名称:keras,代码行数:62,代码来源:test_sequential_model.py

示例4: test_recursive

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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)
开发者ID:jcavalieri8619,项目名称:keras,代码行数:32,代码来源:test_graph_model.py

示例5: train_48calibration_net

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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')
开发者ID:sunbo5439,项目名称:face_detection,代码行数:29,代码来源:Calibration_48.py

示例6: run_mlp

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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
开发者ID:kkamataki,项目名称:kaggle,代码行数:28,代码来源:model_construct.py

示例7: train

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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")
开发者ID:sdlirjc,项目名称:algorithm,代码行数:27,代码来源:tutorial.py

示例8: create_model

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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')
开发者ID:ansrivas,项目名称:keras-rest-server,代码行数:30,代码来源:createpickles.py

示例9: train_model

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
    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
开发者ID:romber2001,项目名称:sentiment_analyze,代码行数:27,代码来源:analyze.py

示例10: main

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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))
开发者ID:lericson,项目名称:pokemonnamegen,代码行数:29,代码来源:train.py

示例11: run_mlp

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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
开发者ID:kkamataki,项目名称:kaggle,代码行数:28,代码来源:gen_test_4_features.py

示例12: trainModel

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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)
开发者ID:HaliteChallenge,项目名称:Halite,代码行数:32,代码来源:TrainMatt.py

示例13: make_model

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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
开发者ID:bpig,项目名称:pickaxe,代码行数:28,代码来源:train.py

示例14: create_neural_network

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
 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
开发者ID:jsupratman13,项目名称:workspace,代码行数:33,代码来源:dnddqn.py

示例15: load_model

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import to_json [as 别名]
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
开发者ID:ducminhkhoi,项目名称:Image-Captioning,代码行数:61,代码来源:main.py


注:本文中的keras.models.Sequential.to_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。