我解決了它。
,能夠通過執行以下操作來重新初始化權重: 重要的部分是SET_VALUE接收所述會話,tensorflow變量和新的權重看重
def _convert_string_dtype(dtype):
if dtype == 'float16':
return tf.float16
if dtype == 'float32':
return tf.float32
elif dtype == 'float64':
return tf.float64
elif dtype == 'int16':
return tf.int16
elif dtype == 'int32':
return tf.int32
elif dtype == 'int64':
return tf.int64
elif dtype == 'uint8':
return tf.int8
elif dtype == 'uint16':
return tf.uint16
else:
raise ValueError('Unsupported dtype:', dtype)
def set_value(sess, x, value):
"""Sets the value of a variable, from a Numpy array.
# Arguments
x: Tensor to set to a new value.
value: Value to set the tensor to, as a Numpy array
(of the same shape).
"""
value = np.asarray(value)
tf_dtype = _convert_string_dtype(x.dtype.name.split('_')[0])
if hasattr(x, '_assign_placeholder'):
assign_placeholder = x._assign_placeholder
assign_op = x._assign_op
else:
assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape)
assign_op = x.assign(assign_placeholder)
x._assign_placeholder = assign_placeholder
x._assign_op = assign_op
return sess.run(assign_op, feed_dict={assign_placeholder: value})
# Tensorflow variable name
tf_var_name ="h2_weights"
var = [var for var in tf.global_variables() if var.op.name==tf_var_name][0]
var_shape = var.get_shape().as_list()
# Initialize to zero
new_weights = np.zeros(var_shape)
set_value(sess,var,new_weights)
來源
2017-03-25 17:38:52
Lee