保存NN的权重和偏差,以便稍后加载



我正试图从Tensorflow 1.4中编写的NN中保存权重和偏差。类内部的NN看起来像

class Model:
def __init__(self):
self.__build_flag = -1
self.__train_flag = -1
def __model(self, x):
# Parameters
dim = self.__dim
hdim = self.__hdim
ddim = self.__ddim
kmatdim = ddim + 1 + dim
num_layers = self.__num_layers
std = 1.0 / np.sqrt(hdim)
std_proj = 1.0 / np.sqrt(dim)
with tf.variable_scope("Input_projection", 
initializer=tf.orthogonal_initializer()):
P = tf.get_variable(name='weights',
shape=(dim,hdim),
dtype=tf.float64)
res_in = tf.matmul(x, P)
with tf.variable_scope("Residual"):
for j in range(self.__num_layers):
layer_name = "Layer_"+str(j)
with tf.variable_scope(layer_name):
W = tf.get_variable(name="weights", shape=(hdim,hdim),
dtype=tf.float64)
b = tf.get_variable(name="biases", shape=(hdim),
dtype=tf.float64)
if j==0: # first layer
res_out = res_in + self.__tf_nlr(
tf.matmul(res_in, W) + b)
else: # subsequent layers
res_out = res_out + self.__tf_nlr(
tf.matmul(res_out, W) + b)
with tf.variable_scope("Output_projection",
initializer=tf.orthogonal_initializer()):                         
W = tf.get_variable(name="weights", shape=(hdim, ddim),
dtype=tf.float64)
b = tf.get_variable(name="biases", shape=(1,ddim),
dtype=tf.float64)
out = tf.matmul(res_out, W) + b
self.weights = W
self.b = b
self.out=out

return out
@property
def save_weights(self):
assert self.__build_flag == 0, "Run build() first."
return self.weights

使用self.weightsself.b,我在实例属性中转换了它们,因为它们可以在整个类中访问。

如果我在Jupiter笔记本上打印重量,它们看起来像这样:

[[ 0.26774276 -0.2249028   0.37067945]
[ 0.38260571 -0.3644912   0.00079572]
[ 0.35924226  0.13907322  0.3965674 ]
[ 0.07286168 -0.39342116  0.04337081]
[-0.30477155 -0.33258672  0.43377254]]

现在,我正在努力拯救他们。我曾想过将它们保存在CSV文件中,稍后再重新加载

np.savetxt("dict_weights.csv", model.save_weights, delimiter=",")

但是,我得到以下错误


----> 7 np.savetxt("dict_weights.csv", model.save_weights, delimiter=",")
8 # np.savetxt("dict_b.csv", model.save_b, delimiter=",")
9 
<__array_function__ internals> in savetxt(*args, **kwargs)
~/opt/anaconda3/envs/edmd_DL_new/lib/python3.6/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)
1370         if X.ndim == 0 or X.ndim > 2:
1371             raise ValueError(
-> 1372                 "Expected 1D or 2D array, got %dD array instead" % X.ndim)
1373         elif X.ndim == 1:
1374             # Common case -- 1d array of numbers
ValueError: Expected 1D or 2D array, got 0D array instead

我找到了方法,

我没有将权重和偏差保存为CSV,而是使用了:

tf.train.Saver

https://github.com/tensorflow/docs/blob/74f7b69f3e7b659bd82146c3db8c68c69a2705c0/site/en/r1/guide/saved_model.md

相关内容

  • 没有找到相关文章

最新更新