我想您是在问如何在工作模型中结合 ReLU?两个选项如下所示:
选项 1) 将 ReLU1 输入到 ReLU2
这可能是首选方法。请注意,r1是r2的输入。
x = tf.placeholder('float', shape=[None, 1])
y_ = tf.placeholder('float', shape=[None, 1])
W1 = weight_variable([1, hidden_units])
b1 = bias_variable([hidden_units])
r1 = tf.nn.relu(tf.matmul(x, W1) + b1)
# Input of r1 into r2 (which is just y)
W2 = weight_variable([hidden_units, 1])
b2 = bias_variable([1])
y = tf.nn.relu(tf.matmul(r1,W2)+b2) # ReLU2
选项 2) 添加 ReLU1 和 ReLU2
选项 2 列在原始问题中,但我不知道这是否是您真正想要的……阅读下面的完整工作示例并尝试一下。我想你会发现它的模型不好。
x = tf.placeholder('float', shape=[None, 1])
y_ = tf.placeholder('float', shape=[None, 1])
W1 = weight_variable([1, hidden_units])
b1 = bias_variable([hidden_units])
r1 = tf.nn.relu(tf.matmul(x, W1) + b1)
# Add r1 to r2 -- won't be able to reduce the error.
W2 = weight_variable([1, hidden_units])
b2 = bias_variable([hidden_units])
r2 = tf.nn.relu(tf.matmul(x, W2) + b2)
y = tf.add(r1,r2) # Again, ReLU2 is just y
完整的工作示例
下面是一个完整的工作示例。默认情况下,它使用选项 1,但是,选项 2 也包含在注释中。
from __future__ import print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# Config the matlotlib backend as plotting inline in IPython
%matplotlib inline
episodes = 55
batch_size = 5
hidden_units = 10
learning_rate = 1e-3
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# Produce the data
x_data = np.arange(-100, 100).astype(np.float32)
y_data = np.abs(x_data + 20.)
# Plot it.
plt.plot(y_data)
plt.ylabel('y_data')
plt.show()
# Might want to randomize the data
# np.random.shuffle(x_data)
# y_data = np.abs(x_data + 20.)
# reshape data ...
x_data = x_data.reshape(200, 1)
y_data = y_data.reshape(200, 1)
# create placeholders to pass the data to the model
x = tf.placeholder('float', shape=[None, 1])
y_ = tf.placeholder('float', shape=[None, 1])
W1 = weight_variable([1, hidden_units])
b1 = bias_variable([hidden_units])
r1 = tf.nn.relu(tf.matmul(x, W1) + b1)
# Input of r1 into r2 (which is just y)
W2 = weight_variable([hidden_units, 1])
b2 = bias_variable([1])
y = tf.nn.relu(tf.matmul(r1,W2)+b2)
# OPTION 2
# Add r1 to r2 -- won't be able to reduce the error.
#W2 = weight_variable([1, hidden_units])
#b2 = bias_variable([hidden_units])
#r2 = tf.nn.relu(tf.matmul(x, W2) + b2)
#y = tf.add(r1,r2)
mean_square_error = tf.reduce_sum(tf.square(y-y_))
training = tf.train.AdamOptimizer(learning_rate).minimize(mean_square_error)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
min_error = np.inf
for _ in range(episodes):
# iterrate trough every row (with batch size of 1)
for i in range(x_data.shape[0]-batch_size+1):
_, error = sess.run([training, mean_square_error], feed_dict={x: x_data[i:i+batch_size], y_:y_data[i:i+batch_size]})
if error < min_error :
min_error = error
if min_error < 3:
print(error)
#print(error)
#print(error, x_data[i:i+batch_size], y_data[i:i+batch_size])
# error = sess.run([training, mean_square_error], feed_dict={x: x_data[i:i+batch_size], y_:y_data[i:i+batch_size]})
# if error != None:
# print(error)
sess.close()
print("\n\nmin_error:",min_error)
在这里的木星笔记本中可能更容易看到