【问题标题】:What is the equivalent of the following tensorflow snippet in CNTKCNTK 中以下 tensorflow 片段的等价物是什么
【发布时间】:2017-04-12 22:45:13
【问题描述】:

我正在尝试在 CNTK 中实现 DDPG 并遇到以下代码(使用 Tensorflow)来创建评论网络:

state_input = tf.placeholder("float",[None,state_dim])
action_input = tf.placeholder("float",[None,action_dim])

W1 = self.variable([state_dim,layer1_size],state_dim)
b1 = self.variable([layer1_size],state_dim)
W2 = self.variable([layer1_size,layer2_size],layer1_size+action_dim)
W2_action = self.variable([action_dim,layer2_size],layer1_size+action_dim)
b2 = self.variable([layer2_size],layer1_size+action_dim)
W3 = tf.Variable(tf.random_uniform([layer2_size,1],-3e-3,3e-3))
b3 = tf.Variable(tf.random_uniform([1],-3e-3,3e-3))

layer1 = tf.nn.relu(tf.matmul(state_input,W1) + b1)
layer2 = tf.nn.relu(tf.matmul(layer1,W2) + tf.matmul(action_input,W2_action) + b2)
q_value_output = tf.identity(tf.matmul(layer2,W3) + b3)

其中 self.variable 定义为:

def variable(self,shape,f):
    return tf.Variable(tf.random_uniform(shape,-1/math.sqrt(f),1/math.sqrt(f)))

忽略随机初始化(我只想要结构),我尝试了以下:

state_in = cntk.input(state_dim, dtype=np.float32)
action_in = cntk.input_variable(action_dim, dtype=np.float32)

W1 = cntk.parameter(shape=(state_dim, layer1_size))
b1 = cntk.parameter(shape=(layer1_size))
W2 = cntk.parameter(shape=(layer1_size, layer2_size))
W2a = cntk.parameter(shape=(action_dim, layer2_size))
b2 = cntk.parameter(shape=(layer2_size))
W3 = cntk.parameter(shape=(layer2_size, 1))
b3 = cntk.parameter(shape=(1))

l1 = cntk.relu(cntk.times(state_in, W1) + b1)
l2 = cntk.relu(cntk.times(l1, W2) + cntk.times(action_in, W2a) + b2)
Q = cntk.times(l2, W3) + b3

但是,layer2 的初始化失败并出现以下错误(sn-p):

RuntimeError: Operation 'Plus': Operand 'Output('Times24_Output_0', [#, *], [300])' 具有动态轴,与动态轴不匹配 '[#]' 的其他操作数。

我想知道我做错了什么以及如何准确地重新创建相同的模型。

【问题讨论】:

    标签: python cntk


    【解决方案1】:

    原因是您将state_in 定义为cntk.inputaction_in 定义为cntk.input_variable,默认情况下它们的类型略有不同:cntk.input 默认创建一个不能绑定到序列数据的变量,而cntk.input_variable 默认创建一个必须绑定到序列数据的变量(注意,input_variable 已弃用,一些 IDE (如 PyCharm)会用删除线显示这一点,请使用 cntk.input() 或 cntk.sequence.input()) .

    错误表示加号操作无法将具有动态轴 [#](表示小批量维度)的 cntk.times(action_in, W2a) 与具有动态轴 [#、*](表示小批量和序列维度)的cntk.times(action_in, W2a) 相加。

    最简单的解决方法是声明 action_in = cntk.input(action_dim, dtype=np.float32) 这使得其余的操作类型检查。

    【讨论】:

    • 谢谢!我的挫败感一定遮住了我的视线。
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多