ef initialize_parameters(): tf.set_random_seed(1) W1 = tf.get_variable("W1", shape=[4, 4, 3, 8], initializer=tf.contrib.layers.xavier_initializer(seed=0)) W2 = tf.get_variable("W2", shape=[2, 2, 8, 16], initializer=tf.contrib.layers.xavier_initializer(seed=0)) parameters = {"W1": W1, "W2": W2} return parameters tf.reset_default_graph() with tf.Session() as sess_test: parameters = initialize_parameters() init = tf.global_variables_initializer() sess_test.run(init) print("W1 = " + str(parameters["W1"].eval()[1,1,1])) print("W2 = " + str(parameters["W2"].eval()[1,1,1])) 请根据现在python版本纠正这段代码
时间: 2024-04-14 09:26:52 浏览: 119
initialize_plotting_parameters.rar_fdtd_fdtd MATLAB
根据最新的Python版本,你需要进行以下修改:
```python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
def initialize_parameters():
tf.set_random_seed(1)
initializer = tf.initializers.GlorotUniform(seed=0)
W1 = tf.get_variable("W1", shape=[4, 4, 3, 8], initializer=initializer)
W2 = tf.get_variable("W2", shape=[2, 2, 8, 16], initializer=initializer)
parameters = {"W1": W1, "W2": W2}
return parameters
tf.reset_default_graph()
with tf.Session() as sess_test:
parameters = initialize_parameters()
init = tf.global_variables_initializer()
sess_test.run(init)
print("W1 = " + str(parameters["W1"].eval()[1, 1, 1]))
print("W2 = " + str(parameters["W2"].eval()[1, 1, 1]))
```
这段代码使用了最新的TensorFlow版本,并且使用了`tf.initializers.GlorotUniform`进行参数初始化。
阅读全文