简体   繁体   English

如何获得模型创建的训练权重

[英]How to get the trained weights created by a model

I implemented a simple logistic regression. 我实现了一个简单的逻辑回归。 Before running the training algorithm, I created a placeholder for my weights where I initialized all the weights to 0... 在运行训练算法之前,我为权重创建了一个占位符,将所有权重初始化为0 ...

W = tf.Variable(tf.zeros([784, 10]))

After initializing all my variables correctly, the logistic regression is implemented (which I've tested and runs correctly)... 正确初始化所有变量后,便实现了逻辑回归(已测试并正确运行)...

for epoch in range(training_epochs):
    avg_cost = 0
    total_batch = int(mnist.train.num_examples/batch_size)
    # loop over all batches
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})

        # compute average loss
        avg_cost += c / total_batch
    # display logs per epoch step
    if (epoch + 1) % display_step == 0:
        print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))

My issue is, I need to extract the weights used in the model. 我的问题是,我需要提取模型中使用的权重。 I used the following for my model... 我为模型使用了以下内容...

pred = tf.nn.softmax(tf.matmul(x, W) + b)  # Softmax

I tried extracting the following way... 我尝试通过以下方式提取...

var = [v for v in tf.trainable_variables() if v.name == "Variable:0"][0]
print(sess.run(var[0]))

I thought that the trained weights would be located in tf.training_variables() , however when I run the print function, I get an array of zeroes. 我以为训练后的权重将位于tf.training_variables() ,但是当我运行print函数时,会得到一个零数组。

What I want, is the all sets of weights. 我想要的是所有的砝码。 But for some reason I am getting arrays of zeroes instead of the actual weights of the classifier. 但是由于某种原因,我得到的是零数组,而不是分类器的实际权重。

The variable W should refer to the trained weights. 变量W应参考训练后的权重。 Please try simply doing: sess.run(W) 请尝试简单地做: sess.run(W)

简单得多,只需使用run函数评估权重,您将获得具有值的numpy数组:

sess.run([x, W, b])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存经过训练的 tensorflow model 的结构和权重? - How to save the structure and weights of trained tensorflow model? 如何通过设置权重创建训练有素的Keras模型 - How to Create a Trained Keras Model Through Setting the Weights 如何访问和可视化预训练的 TensorFlow 2 模型中的权重? - How to access and visualize the weights in a pre-trained TensorFlow 2 model? 如何可视化 keras model 中所有层的学习训练权重? - How to visualize the learned trained weights in a keras model for all of its layers? 在 TensorFlow 模型中重用经过训练的权重而无需重新初始化 - Reuse trained weights in TensorFlow model without reinitialization 如何加载训练模型的权重,这些权重是用 tf.keras.models.save_model 保存的? - How to load a trained model's weights, which were saved with tf.keras.models.save_model? 如何使用预训练的模型权重初始化新的 word2vec 模型? - How to initialize a new word2vec model with pre-trained model weights? 如何在 Pytorch 中获得部分预训练的 model? - How to get part of pre trained model in Pytorch? 如何获得“得分”训练好的模型(预测) - How to get a “scoring” trained model(predict) 如何在Keras中加载卷积神经网络前几层的权重并删除预训练的model? - How to load the weights of the first few layers of Convolutional Neural Network in Keras and delete the pre-trained model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM