使用numpy权重保存和恢复tensorflow模型

时间:2018-03-26 19:44:27

标签: numpy math tensorflow

请帮我用numpy方式恢复简单的张量流模型,这里是:

x = tf.placeholder(tf.float32,shape=[None,196])

# 19 because 0-18 possible numbers
W = tf.Variable(tf.zeros([196,19]))
b = tf.Variable(tf.zeros([19]))

# Create the Graph
y = tf.matmul(x,W) + b 

# Loss and Optimizer
y_true = tf.placeholder(tf.float32,[None,19])

# Cross Entropy
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=2.5)

train = optimizer.minimize(cross_entropy)

### Create Session
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    # Train the model for 1000 steps on the training set
    # Using built in batch feeder
    def batch(array:"[[]]"=[], step:"int"= 2):
        for i in range(0,len(array),step):
            yield  array[i:i+step]

    batch_generator_x = batch(x_train, step = 100)
    batch_generator_y = batch(y_train, step = 100)

    while True:     
        try:
            batch_x = batch_generator_x.__next__()
            batch_y = batch_generator_y.__next__()
            sess.run(train,feed_dict={x:batch_x,y_true:batch_y})
        except StopIteration:
            break

    # Test the Train Model
    matches = tf.equal(tf.argmax(y,1),tf.argmax(y_true,1))
    acc = tf.reduce_mean(tf.cast(matches,tf.float32))

    print(sess.run(acc,feed_dict={x:x_eval,y_true:y_eval}))

在这里,我保存了我的模型矩阵权重和偏差:

    W_val, b_val = sess.run([W, b])

    np.savetxt("W.csv", W_val, delimiter=",")
    np.savetxt("b.csv", b_val, delimiter=",")

然后我想我应该这样做恢复:

W = np.loadtxt("W.csv",delimiter=",").astype(np.float32)
b = np.loadtxt("b.csv",delimiter=",").astype(np.float32)

x = x_eval[0] # "image" example

y = x*W + b #prediction

但如何预测W.shape - (196,19)和x.shape - (196,)

我理解为什么这个#ValueError: operands could not be broadcast together with shapes (196,) (196,19)但是这个操作张量流是怎么做的?

1 个答案:

答案 0 :(得分:1)

首先,我强烈建议您使用tensorflow的检查点保存和恢复模型,它比您尝试的更容易:

https://www.tensorflow.org/programmers_guide/saved_model

但是如果你真的想手动导出和导入权重(你绝对可以做),你需要理解tensorflow的声明性编程方法。所有张量流操作和张量(变量)都是符号的,存在于称为计算图的容器中。你不能设置W = np.loadtxt(...),因为你试图将W设置为数值,但W是一个符号,它实际上是一个python对象,它只代表一些可能的数据或者可能不存在于记忆中。它是 NOT 数据。

现在,按照你所描述的方式进行操作,你需要定义一个占位符来保存你传入的权重,然后定义一个赋值操作,然后通过sess.run与整个事物进行交互。 。这里有一些伪造的代码,我还没有通过测试来说明:

weights_placeholder = tf.placeholder(some_appropriate_shape)
assign_op = tf.assign(W, weights_placeholder)

sess.run(assign_op, feed_dict={weights_placeholder: np.loadtxt(...)})