當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。