Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在开始时声明固定权重_Python_Tensorflow - Fatal编程技术网

Python 在开始时声明固定权重

Python 在开始时声明固定权重,python,tensorflow,Python,Tensorflow,当我有2个输入、1个隐藏层、3个节点和1个输出时,如何在开始时声明固定权重 因此,输入权重必须有6个固定值,输出权重必须有3个固定值 weights_input=tf.Variable([wi1],[wi2],[wi3],[wi4],[wi5],[wi6]) weights_output=tf.Variable([wo1],[wo2],[wo3]) 可能是这样吧?您可以通过以下方法解决此问题 from tensorflow.keras.layers import Dense from ten

当我有2个输入、1个隐藏层、3个节点和1个输出时,如何在开始时声明固定权重

因此,输入权重必须有6个固定值,输出权重必须有3个固定值

weights_input=tf.Variable([wi1],[wi2],[wi3],[wi4],[wi5],[wi6])

weights_output=tf.Variable([wo1],[wo2],[wo3])

可能是这样吧?

您可以通过以下方法解决此问题

from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

model = Sequential([
                    Dense(3),
                    Dense(1)
]) 

model.compile(loss='mse') 
model.build(input_shape=[2,1])

**Model Summary:** 

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              multiple                  6         
_________________________________________________________________
dense_5 (Dense)              multiple                  4         
=================================================================
Total params: 10
Trainable params: 10
Non-trainable params: 0  
对于隐藏层,它有10个参数、9个权重和1个偏差

现在,让我们定义输入和输出层的权重

import numpy as np
weights_input=[np.array([[1., 1., 1.]]), np.array([1, 1, 1])]

weights_output=[np.array([[1.],[1.],[1.]]), np.array([1])]  
让我们看看每个层的默认权重

model.layers[0].get_weights()  
输出: [数组([[0.04900634,-0.6587564,-0.15010011]],数据类型=float32), 数组([0,0,0.],dtype=float32)]

输出:

[array([[1.],[1.],[1.]], dtype=float32), array([1.], dtype=float32)]
[数组([-0.3019548]、-0.9051015]、[0.23144984]、dtype=float32)、数组([0.],dtype=float32)]

让我们为两层设置权重

model.layers[0].set_weights(weights_input)
model.layers[1].set_weights(weights_output) 
如果我们现在看看每层的重量

model.layers[0].get_weights()  
输出:

[array([[1., 1., 1.]], dtype=float32), array([1., 1., 1.], dtype=float32)]
对于输出层:

输出:

[array([[1.],[1.],[1.]], dtype=float32), array([1.], dtype=float32)]

您可以通过以下方法解决此问题

from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

model = Sequential([
                    Dense(3),
                    Dense(1)
]) 

model.compile(loss='mse') 
model.build(input_shape=[2,1])

**Model Summary:** 

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              multiple                  6         
_________________________________________________________________
dense_5 (Dense)              multiple                  4         
=================================================================
Total params: 10
Trainable params: 10
Non-trainable params: 0  
对于隐藏层,它有10个参数、9个权重和1个偏差

现在,让我们定义输入和输出层的权重

import numpy as np
weights_input=[np.array([[1., 1., 1.]]), np.array([1, 1, 1])]

weights_output=[np.array([[1.],[1.],[1.]]), np.array([1])]  
让我们看看每个层的默认权重

model.layers[0].get_weights()  
输出: [数组([[0.04900634,-0.6587564,-0.15010011]],数据类型=float32), 数组([0,0,0.],dtype=float32)]

输出:

[array([[1.],[1.],[1.]], dtype=float32), array([1.], dtype=float32)]
[数组([-0.3019548]、-0.9051015]、[0.23144984]、dtype=float32)、数组([0.],dtype=float32)]

让我们为两层设置权重

model.layers[0].set_weights(weights_input)
model.layers[1].set_weights(weights_output) 
如果我们现在看看每层的重量

model.layers[0].get_weights()  
输出:

[array([[1., 1., 1.]], dtype=float32), array([1., 1., 1.], dtype=float32)]
对于输出层:

输出:

[array([[1.],[1.],[1.]], dtype=float32), array([1.], dtype=float32)]

这是什么语言?这和xor有什么关系?Python和Tensorflow。大多数人使用随机权重,但我想用固定权重追踪第一个时代。这是什么语言?这和xor有什么关系?Python和Tensorflow。大多数人使用随机权重,但我想用固定权重追踪第一个时代。@Ramataklan-如果你认为我回答了你的问题,请接受并投票,谢谢。@Ramataklan-如果你认为我回答了你的问题,请接受并投票,谢谢。