【问题标题】:Tensorflow: variable number of levels in a neural net modelTensorflow:神经网络模型中的可变级别数
【发布时间】:2017-08-26 13:43:21
【问题描述】:

我想知道是否有一种方法可以使用 tensorflow (python) 在模型中实现可变数量的隐藏层。我问这个是因为我不知道我必须实现多少级别,我必须读取一个文件或一个变量才能获取该信息。
这就是我想做的事情:

w = tf.Variable(tf.random_normal([h, w]))
self.__encoder[index] = tf.matmul(label, w)

所以__encoder的每一层都包含一个隐藏层,但我不知道它是否有效,也不知道如何实现训练。

【问题讨论】:

  • 请提供一个完整的例子。我不清楚你的意思是隐藏层的数量取决于输入。
  • 只需从变量中读取隐藏层的数量或从输入中读取数字,然后我必须创建执行简单神经元的隐藏层数量。

标签: python tensorflow layer


【解决方案1】:

在给你一个理由之前,我想说 tensorflow 是为静态计算图而生的。所以,这个答案是为了做到这一点:

  1. 从文件中读取一定数量
  2. 使用该数量的隐藏层构建模型
  3. 使用此模型并提供一些其他输出

我仍然不清楚您为什么要在某处读取隐藏层的数量,其中隐藏(或潜在)意味着这些是不依赖于您的数据的超参数。

无论如何,根据一个变量构建不同数量的层就是这样:

def layer(x, input_size, output_size):
    w = tf.Variable(tf.truncated_normal(shape=[input_size, output_size]))
    b = tf.Variable(tf.zeros([output_size]))
    return tf.sigmoid(tf.matmul(x,w) + b)

num_layers = ... #read somewhere
size_layers = ... #output_dimesion for each layer
h = x
for i in range(num_layers):
    h = layer(h, input_size, size_layers[i])
    input_size = size_layer[i]

循环之后,h有最后一层的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多