python - 复制具有相同属性的 tensorflow 层以形成图

标签 python machine-learning computer-vision tensorflow deep-learning

我尝试使用神经网络实现的任务与其最常见的用法略有不同。我尝试通过优化代表物理属性的网络权重,将某些内容从输入层传播到输出层来模拟物理过程。

因此,我需要一个 150 层网络,其中每一层都具有相同的属性,格式为

mx+b

其中 x 是我喜欢优化的变量,m 是每个层都相同的外部因素(b 目前未使用)。

我想自动化创建图表的过程,而不是复制/粘贴每个图层。那么有没有一个函数可以将第一层的结构复制到后面的所有层呢?

在 tensorflow 中,它应该看起来像这样:

graph = tf.Graph()
with graph.as_default():

    # Input data.
    tf_input = tf.placeholder(tf.float32, shape=(n_data, n))
    tf_spatial_grid = tf.constant(m_index_mat)
    tf_ph_unit = tf.const(m_unit_mat)
    tf_output = tf.placeholder(tf.float32, shape=(n_data, n))

    # new hidden layer 1    
    hidden_weights = tf.Variable( tf.truncated_normal([n*n, 1]) )
    hidden_layer = tf.nn.matmul( tf.matmul( tf_input, hidden_weights), tf_ph_unit)

    # new hidden layer 2
    hidden_weights_2 = tf.Variable( tf.truncated_normal([n*n, 1]) )
    hidden_layer_2 = tf.nn.matmul( tf.matmul( hidden_layer, hidden_weights_2), tf_ph_unit)

……

    # new hidden layer n
    hidden_weights_n = tf.Variable( tf.truncated_normal([n*n, 1]) )
    hidden_layer_n = tf.nn.matmul( tf.matmul( hidden_layer_m, hidden_weights_n), tf_ph_unit)

...

那么有什么选项可以以某种方式自动执行此过程吗?也许我错过了一些东西

我真的很感谢任何帮助!

最佳答案

实现这一点的最简单方法是创建一个构建层的函数,并简单地多次调用该函数(可能是在循环中)。

例如:

def layer(input):
    hidden_weights = tf.Variable( tf.truncated_normal([n*n, 1]) )
    hidden_layer = tf.nn.matmul( tf.matmul( input, hidden_weights), tf_ph_unit)
    return hidden_layer

然后:

input = tf_input
for i in range(10):
    input = layer(input)

关于python - 复制具有相同属性的 tensorflow 层以形成图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271585/

相关文章:

opencv - 如何使用OpenCV人脸识别模块获得更好的结果

api - Microsoft 计算机视觉 API 或 Google 的 Cloud Vision API 是否可以获取对象的位置?

python - 如何在 HoughLinesP 之后合并行?

r - bnlearn 节点的错误尺寸

python - 加载视频数据集(Keras)

python - 从命令行运行函数并将参数传递给函数

python - `list(itertools.groupy)` 的违反直觉的行为

python - 如何在 pyspark 2.3 中的二元问题(BinaryClassificationEvaluator)中为 CrossValidator 评估器使用 f1-score

math - 为什么机器学习不能识别素数?

python - Google App Engine 兼容层