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


Python Sequential.from_config方法代码示例

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


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

示例1: _load_model_config

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def _load_model_config(model_cfg, model_weights):
        if type(model_cfg) == str:
            if not os.path.exists(model_cfg):
                try:
                    class_ = getattr(applications, model_cfg)
                    return class_(weights=model_weights)
                except AttributeError:
                    available_mdls = [attr for attr in dir(applications) if callable(getattr(applications, attr))]
                    raise ValueError('Could not load pretrained model with key {}. '
                                     'Available models: {}'.format(model_cfg, ', '.join(available_mdls)))

            with open(model_cfg, 'r') as fileh:
                try:
                    return model_from_json(fileh)
                except ValueError:
                    pass

                try:
                    return model_from_yaml(fileh)
                except ValueError:
                    pass

            raise ValueError('Could not load model from configuration file {}. '
                             'Make sure the path is correct and the file format is yaml or json.'.format(model_cfg))
        elif type(model_cfg) == dict:
            return Model.from_config(model_cfg)
        elif type(model_cfg) == list:
            return Sequential.from_config(model_cfg)

        raise ValueError('Could not load model from configuration object of type {}.'.format(type(model_cfg))) 
开发者ID:mlosch,项目名称:emu,代码行数:32,代码来源:keras.py

示例2: build_model_from_file

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def build_model_from_file(model_file):
    structure, weights = pickle.load(open(model_file, 'rb'))
    model = Sequential.from_config(structure)
    model.set_weights(weights)

    return model 
开发者ID:Linusp,项目名称:soph,代码行数:8,代码来源:utils.py

示例3: build_inference_model

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def build_inference_model(model, batch_size=1, seq_len=1):
    """
    build inference model from model config
    input shape modified to (1, 1)
    """
    logger.info("building inference model.")
    config = model.get_config()
    # edit batch_size and seq_len
    config[0]["config"]["batch_input_shape"] = (batch_size, seq_len)
    inference_model = Sequential.from_config(config)
    inference_model.trainable = False
    return inference_model 
开发者ID:yxtay,项目名称:char-rnn-text-generation,代码行数:14,代码来源:keras_model.py

示例4: __init__

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def __init__(self, S, A, learning_rate=1e-3, gamma=0.9, policy=epsilon_greedy, batch_size=32, update_freq=1000, memory_size=100):
        self.S = S
        self.A = A
        self.learning_rate = learning_rate
        self.gamma = gamma
        self.policy = policy
        self.update_freq = update_freq
        self.batch_size = batch_size

        self.input_dim = self.get_space_dim(S)
        self.output_dim = self.get_space_dim(A)

        ################################
        # Build Model and Target Model #
        ################################
        model = Sequential()
        model.add(Dense(10, input_dim=self.input_dim, name='layer1'))
        model.add(Activation('relu'))
        model.add(Dense(self.output_dim, name='layer2'))

        model.compile(loss='mse', optimizer=sgd(lr=self.learning_rate))
        self.model = model
    
        self.target_model = Sequential.from_config(self.model.get_config())
        self.target_model.set_weights(self.model.get_weights())

        # Build Replay Memory #
        self.replay_memory = ReplayMemory(memory_size, self.input_dim)

        self.state = np.zeros(self.input_dim)
        self.action = 0

        self.time = 0
        self.episode = 0 
开发者ID:aejax,项目名称:KerasRL,代码行数:36,代码来源:dqn.py

示例5: test_nested_sequential

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

示例6: test_merge_sum

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

示例7: test_merge_recursivity

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def test_merge_recursivity():
    (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'))

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

    intermediate = Sequential()
    intermediate.add(Merge([left, right], mode='sum'))
    intermediate.add(Dense(nb_hidden))
    intermediate.add(Activation('relu'))

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

    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
    model.fit([X_train, 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, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, 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, X_test], y_test, verbose=0)

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

    fname = 'test_merge_recursivity_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate([X_test, 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) 
开发者ID:GeekLiB,项目名称:keras,代码行数:56,代码来源:test_sequential_model.py

示例8: test_merge_overlap

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def test_merge_overlap():
    (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'))

    model = Sequential()
    model.add(Merge([left, left], mode='sum'))
    model.add(Dense(nb_class))
    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_merge_overlap_temp.h5'
    print(model.layers)
    model.save_weights(fname, overwrite=True)
    print(model.trainable_weights)

    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:GeekLiB,项目名称:keras,代码行数:47,代码来源:test_sequential_model.py

示例9: get_agent

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def get_agent(env, name=None):
    S = env.observation_space
    A = env.action_space

    memory_size = 50000
    random_start = 500
    exploration_frames = 10000
    epsilon = 0.01
    gamma = 0.99               
    target_update_freq = 1000
    update_freq = 1
    history_len = 1
    batch_size = 32
    loss = huber_loss
    #opt =  RMSprop(lr=0.00025)
    opt = 'adam'
    bounds = False
    double = False
    update_cycles = None
    name = 'sDQN' if name == None else name
    image = False
 
    # define the model
    model = Sequential()
    if type(S) == gym.spaces.Discrete:
        input_dim = history_len*S.n
        model.add(Dense(50, input_dim=input_dim, activation='relu', name='l1'))
    else:
        input_shape = [dim for dim in S.shape]
        input_shape[-1] *= history_len
        model.add(Dense(50, input_shape=input_shape, activation='relu', name='l1'))
    model.add(Dense(100, activation='relu', name='l2'))
    model.add(Dense(50, activation='relu', name='l3'))
    model.add(Dense(A.n, activation='linear', name='l4'))

    config = model.get_config()
    model2 = Sequential.from_config(config)
    model2.set_weights(model.get_weights())

    Q = KerasQ(S, A, model=model, loss=loss, optimizer=opt, bounds=bounds, batch_size=batch_size)
    targetQ = KerasQ(S, A, model=model2, loss=loss, optimizer=opt, bounds=bounds, batch_size=batch_size)
    policy = MaxQ(Q, randomness=SAepsilon_greedy(A, epsilon=epsilon, final=exploration_frames))
    agent = DQN(S, A, gamma=gamma, policy=policy, Qfunction=Q, targetQfunction=targetQ, memory_size=memory_size, random_start=random_start, 
                batch_size=batch_size, target_update_freq=target_update_freq, update_freq=update_freq, history_len=history_len, 
                image=image, name=name, double=double, bounds=bounds, update_cycles=update_cycles)

    return agent 
开发者ID:aejax,项目名称:KerasRL,代码行数:49,代码来源:simple_dqn.py

示例10: get_agent

# 需要导入模块: from keras.models import Sequential [as 别名]
# 或者: from keras.models.Sequential import from_config [as 别名]
def get_agent(env, name=None):
    S = env.observation_space
    A = env.action_space

    memory_size = 1000000
    random_start = 50000
    exploration_frames = 1000000
    epsilon = 0.1
    gamma = 0.99            
    target_update_freq = 10000
    update_freq = 1 # update every 4 steps or 4 frames?
    history_len = 4
    batch_size = 32
    loss = huber_loss
    opt =  RMSprop(lr=0.00025)
    bounds = False
    double = False
    update_cycles = None
    name = 'atariDQN' if name == None else name
    image = True
 
    # define the model
    if K.image_dim_ordering() == 'th':
        state_shape = (4,84,84)
    else:
        state_shape = (84,84,4)
    model = Sequential()
    model.add(Convolution2D(32, 8, 8, subsample=(4,4), input_shape=state_shape, activation='relu', init='he_uniform'))
    model.add(Convolution2D(64, 4, 4, subsample=(2,2), activation='relu', init='he_uniform'))
    model.add(Convolution2D(64, 3, 3, subsample=(1,1), activation='relu', init='he_uniform'))
    model.add(Flatten())
    model.add(Dense(512, activation='relu', init='he_uniform'))
    model.add(Dense(A.n, init='he_uniform'))

    config = model.get_config()
    model2 = Sequential.from_config(config)
    model2.set_weights(model.get_weights())

    Q = KerasQ(S, A, model=model, loss=loss, optimizer=opt, bounds=bounds, batch_size=batch_size)
    targetQ = KerasQ(S, A, model=model2, loss=loss, optimizer=opt, bounds=bounds, batch_size=batch_size)
    policy = MaxQ(Q, randomness=SAepsilon_greedy(A, epsilon=epsilon, final=exploration_frames))
    agent = DQN(S, A, gamma=gamma, policy=policy, Qfunction=Q, targetQfunction=targetQ, memory_size=memory_size, random_start=random_start, 
                batch_size=batch_size, target_update_freq=target_update_freq, update_freq=update_freq, history_len=history_len, 
                image=image, name=name, double=double, bounds=bounds, update_cycles=update_cycles)

    return agent 
开发者ID:aejax,项目名称:KerasRL,代码行数:48,代码来源:atari_dqn.py


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