gpt4 book ai didi

python - tensorflow 中的权重和偏差初始化

转载 作者:行者123 更新时间:2023-11-30 08:58:05 25 4
gpt4 key购买 nike

我正在做一些电力负荷预测,其中我想初始化权重和偏差。我使用不同的算法计算了权重和偏差并将其保存在文件中。我想使用该文件并使用这些权重和偏差开始训练。

这是我要更新的代码。

#RNN designning
tf.reset_default_graph()

inputs = 1 #input vector size
hidden = 100
output = 1 #output vector size

X = tf.placeholder(tf.float32, [None, num_periods, inputs])
y = tf.placeholder(tf.float32, [None, num_periods, output])


basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden, activation=tf.nn.relu)
rnn_output, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)

learning_rate = 0.001 #small learning rate so we don't overshoot the minimum

stacked_rnn_output = tf.reshape(rnn_output, [-1, hidden]) #change the form into a tensor
stacked_outputs = tf.layers.dense(stacked_rnn_output, output) #specify the type of layer (dense)
outputs = tf.reshape(stacked_outputs, [-1, num_periods, output]) #shape of results

loss = tf.reduce_mean(tf.square(outputs - y)) #define the cost function which evaluates the quality of our model
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) #gradient descent method
training_op = optimizer.minimize(loss) #train the result of the application of the cost_function

init = tf.global_variables_initializer() #initialize all the variables
epochs = 1000 #number of iterations or training cycles, includes both the FeedFoward and Backpropogation
mape = []

def mean_absolute_percentage_error(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

y_pred = {'NSW': [], 'QLD': [], 'SA': [], 'TAS': [], 'VIC': []}

for st in state.values():
print("State: ", st, end='\n')
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
init.run()
for ep in range(epochs):
sess.run(training_op, feed_dict={X: x_batches[st], y: y_batches[st]})
if ep % 100 == 0:
mse = loss.eval(feed_dict={X: x_batches[st], y: y_batches[st]})
print(ep, "MSE:", mse)
y_pred[st] = sess.run(outputs, feed_dict={X: x_batches_test[st]})
print("\n")

我使用以下算法查找权重和偏差,并将其保存在 weightsbiases 中作为列表列表。

class network:
def set_weight_bias(self, a):
lIt = 0
rIt = 0
self.weights = []
self.biases = []
for x,y in zip(self.sizes[1:], self.sizes[:-1]):
rIt += x*y
self.weights.append(a[lIt:rIt].reshape((x,y)))
lIt = rIt
for x in self.sizes[1:]:
rIt += x
self.biases.append(a[lIt:rIt].reshape((x,1)))
lIt = rIt

...
"""
Cuckoo Search Optimization
"""

def objectiveFunction(self,x):
self.set_weight_bias(x)
y_prime = self.feedforward(self.input)
return sum(abs(u-v) for u,v in zip(y_prime, self.output))/x.shape[0]

def cso(self, n, x, y, function, lb, ub, dimension, iteration, pa=0.25,
nest=100):
"""
:param n: number of agents
:param function: test function
:param lb: lower limits for plot axes
:param ub: upper limits for plot axes
:param dimension: space dimension
:param iteration: number of iterations
:param pa: probability of cuckoo's egg detection (default value is 0.25)
:param nest: number of nests (default value is 100)
"""
...

我想使用自定义权重和偏差来开始训练,而不是通过 tensorflow 随机分配权重和偏差。在 tensorflow 中如何做到这一点?

最佳答案

您想为 RNN Cell 还是 Dense 层设置权重?如果是 RNN 单元,您应该能够使用 set_weights 设置权重方法。

如果是为了Dense层,您应该能够分配 Variable并使用 initializer 参数来传递你的权重(以及另一个用于偏差的参数)。然后,当您调用layers.dense时,您可以将两个变量张​​量分别传递给kernel_initializerbias_initializer作为权重和偏差。

关于python - tensorflow 中的权重和偏差初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51557648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com