0

感谢您阅读我的问题。

我正在使用 keras 开发基于 keres-rl 的强化学习代理。但我想升级我的代理,以便从开放 AI 基线代码中获得一些更新,以便更好地进行动作探索。但是代码只使用了 tensorflow。这是我第一次使用 tensorflow。我感到很困惑。我使用其“模型 API”构建 keras 深度学习模型。我从不关心模型的内部。但是我引用的代码充满了在深度学习模型内部启动并对其权重进行一些更改并使用 tf.Session() 获得即时层输出的代码。这个框架非常灵活。如下所示,使用 tf.Session() 可识别张量且不可调用的张量可以获得馈送 feed_dict 数据的结果。据我所知,在 keras 中,这是不可能的。

一旦我允许使用 tf.Session(),我的架构就会很复杂,没有人愿意理解和使用它,除非我可以更轻松地调整参考代码。

另一方面,如果我不允许这样做,我需要分解我现有的模型并使用大量的 K.function 来获得中间层的输出或者我无法从 keras 模型中获得的东西。

import numpy as np
from keras.layers import Dense, Input, BatchNormalization
from keras.models import Model
import tensorflow as tf
import keras.backend as K
import rl2.tf_util as U


def normalize(x, stats):
    if stats is None:
        return x
    return (x - stats.mean) / (stats.std + 1e-8)

class RunningMeanStd(object):
    # https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
    def __init__(self, my, epsilon=1e-2, shape=()):

        self._sum = K.variable(value=np.zeros(shape), dtype=tf.float32, name=my+"_runningsum")
        self._sumsq = K.variable(value=np.zeros(shape) + epsilon, dtype=tf.float32, name=my+"_runningsumsq")
        self._count = K.variable(value=np.zeros(()) + epsilon, dtype=tf.float32, name=my+"_count")

        self.mean = self._sum / self._count
        self.std = K.sqrt(K.maximum((self._sumsq / self._count) - K.square(self.mean), epsilon))

        newsum = K.variable(value=np.zeros(shape), dtype=tf.float32, name=my+'_sum')
        newsumsq = K.variable(value=np.zeros(shape), dtype=tf.float32, name=my+'_var')
        newcount = K.variable(value=np.zeros(()), dtype=tf.float32, name=my+'_count')

        self.incfiltparams = K.function([newsum, newsumsq, newcount], [],
            updates=[K.update_add(self._sum, newsum),
                     K.update(self._sumsq, newsumsq),
                     K.update(self._count, newcount)])

    def update(self, x):
        x = x.astype('float64')
        n = int(np.prod(self.shape))
        totalvec = np.zeros(n*2+1, 'float64')
        addvec = np.concatenate([x.sum(axis=0).ravel(), np.square(x).sum(axis=0).ravel(), np.array([len(x)],dtype='float64')])
        self.incfiltparams(totalvec[0:n].reshape(self.shape),
                           totalvec[n:2*n].reshape(self.shape),
                           totalvec[2*n])

i = Input(shape=(1,))
# h = BatchNormalization()(i)
h = Dense(4, activation='relu',  kernel_initializer='he_uniform')(i)
h = Dense(10, activation='relu', kernel_initializer='he_uniform')(h)
o = Dense(1, activation='linear', kernel_initializer='he_uniform')(h)

model = Model(i, o)

obs_rms = RunningMeanStd(my='obs', shape=(1,))
normalized_obs0 = K.clip(normalize(i, obs_rms), 0, 100)

tf2 = model(normalized_obs0)

# print(model.predict(np.asarray([2,2,2,2,2]).reshape(5,)))
# print(tf(np.asarray([2,2,2,2,2]).reshape(5,)))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run([tf2], feed_dict={i : U.adjust_shape(i, [np.asarray([2,]).reshape(1,)])}))
4

0 回答 0