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

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

因此,我需要一个例如150层的网络,每层具有相同的属性,形式为

mx+b

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

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

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

graph = tf.Graph()with graph.as_default():    # 输入数据。    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))    # 新隐藏层 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)    # 新隐藏层 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)

……

    # 新隐藏层 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_inputfor i in range(10):    input = layer(input)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注