简体   繁体   English

串联连接多个神经网络的方式(非并行)

[英]The Way to Connect Multiple Neural Networks in a Series(Not Parallel)

I wonder there is any way to connect multiple NN as a series in tensorflow. 我想知道是否有任何方法可以将多个NN连接为张量流中的一个序列。 For example, input features to DNN structure, and get the result values for input data of RNN structure. 例如,将特征输入到DNN结构,并获取RNN结构的输入数据的结果值。

Example code: 示例代码:

import tensorflow as tf
import numpy as np

a = 50 #batch_size
b = 60 #sequence in RNN
c = 40 #features
d = 6  #label classes
rnn_size = b
x_data = np.random.rand(a,b,c)
y_data = np.random.randint(0,high=d,size=[a,1])

tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None,b,c])
Y = tf.placeholder(tf.float32, shape=[None,d])
X = tf.transpose(X, (1,0,2))
X = tf.reshape(X, (-1,c))
X = tf.split(X, b)
hidden_units = [40,20,10]

#DNN Structure
dnn = []
for i in range(len(hidden_units)):
    if i == 0:
        T = X
    else:
        T = dnn[-1]
    dnn.append(tf.layers.dense(T, hidden_units[i], activation=tf.nn.relu, kernel_initializer=tf.contrib.layers.xavier_initializer()))

# RNN Structure
rnn = {'w': tf.Variable(tf.random_normal([rnn_size, d], stddev = 0.01), dtype=tf.float32),
       'b': tf.Variable(tf.random_normal([d], stddev = 0.01), dtype=tf.float32)}
cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size)
outputs, states = tf.contrib.rnn.static_rnn(cell, dnn[-1], dtype=tf.float32)
output = tf.matmul(outputs[-1], rnn['w'])+rnn['b']

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y,logits=output))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
correct = tf.equal(tf.argmax(output,1),tf.argmax(cost,1))
acc = tf.reduce_mean(tf.cast(correct, tf.float32))

# Run Session
sess = tf.Session()
sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
_, c = sess.run([optimizer, cost],feed_dict={X: x_data, Y: tf.Session().run(tf.one_hot(y_data), d)})
print('Accuracy: ', sess.run(acc, feed_dict={X: x_data, Y: tf.Session().run(tf.one_hot(y_data), d)}))

When I run this code, there is an error raised: 当我运行此代码时,出现错误:

File "C:\Anaconda3\Lib\site-packages\tensorflow\python\layers\core.py", line 250, in dense
dtype=inputs.dtype.base_dtype,

AttributeError: 'list' object has no attribute 'dtype'

it seems to be related with type of 'dnn[-1]' 它似乎与'dnn [-1]'的类型有关

Is there a connective function or data type controller for the connection of the neural networks? 是否有用于神经网络连接的连接函数或数据类型控制器?

I've solved the problem, finally. 我终于解决了这个问题。 The reason of error was little bit ambiguous but X was recognized by 'list' after running 'tf.split', finitely... After I generate a list of DNN Structures which as a length of the sequence, as following: seq = [] for i in range(b): ###dnn structure for i-th array of split### seq.append(dnn structure) 错误的原因有点模棱两可,但是在运行“ tf.split”后,X可以被“ list”识别,这是有限的……在我生成了DNN结构列表之后,该列表作为序列的长度,如下所示: ] for range(b)中的i:### dnn结构,用于拆分的第i个数组### seq.append(dnn结构)

and tuned some codes, then the whole code worked well. 并调整了一些代码,然后整个代码运行良好。 Thanks for an attention :) 感谢您的关注:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM