[英]I tried to predict an image to my pretrained deep learning classification model
When I test with validation data the output returned be with that shape [7.2918165e-06 3.2451030e-02 9.6753991e-01 1.7616502e-06] when I try to send a single image after I save the model and predict it the output be in shape [0.当我使用验证数据进行测试时,当我在保存模型并预测输出后尝试发送单个图像时,返回的输出具有该形状 [7.2918165e-06 3.2451030e-02 9.6753991e-01 1.7616502e-06]形状 [0. 1. 0. 0.] what is the problem
1. 0. 0.] 有什么问题
With the model.predict( ) is supposed to return the same shape as the model output with the valid values co-responsive from the input, and current training weights with a small change in the process when it is trainable.使用 model.predict( ) 应该返回与模型输出相同的形状,其有效值与输入共同响应,并且当前训练权重在可训练的过程中具有微小的变化。
The output of the last layer with activation, target layer output absorbed optimizer or loss Fn.具有激活的最后一层的输出,目标层输出吸收优化器或损失 Fn。 Some of the output is binary, entropy, logits, or multiple values when you working with layer output.
当您使用层输出时,一些输出是二进制、熵、logits 或多个值。 ( see the shape is [ 2, 1 ] )
(看形状是 [ 2, 1 ] )
[ Sample ]: [ 样本 ]:
from os.path import exists
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
learning_rate = 0.001
global_step = 0
vocabulary_size = 5000
start = 0
limit = 128
delta = 1
embedding_size = 16
n_sample = 16
tf.compat.v1.disable_eager_execution()
# Input data.
inputs = tf.compat.v1.get_variable('X', dtype = tf.int32, initializer = tf.random.uniform(shape=[1], maxval=1, dtype=tf.int32, seed=10))
labels = tf.compat.v1.get_variable('Y', dtype = tf.int32, initializer = tf.random.uniform(shape=[1, 1], maxval=1, dtype=tf.int32, seed=10))
# Look up embeddings for inputs.
embeddings = tf.Variable(
tf.random.uniform([vocabulary_size, embedding_size], -1.0, 1.0)
)
embed = tf.nn.embedding_lookup(embeddings, inputs)
# Construct the variables for the NCE loss
nce_weights = tf.Variable(
tf.random.uniform(shape=[vocabulary_size, embedding_size], maxval=255, dtype=tf.float32,)
)
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
# time we evaluate the loss.
loss = tf.reduce_mean(
tf.nn.nce_loss(nce_weights, nce_biases,
labels=labels,
inputs=embed,
num_sampled=n_sample,
num_classes=vocabulary_size),
name='loss'
)
optimizer = tf.compat.v1.train.ProximalAdagradOptimizer(
learning_rate,
initial_accumulator_value=0.1,
l1_regularization_strength=0.2,
l2_regularization_strength=0.1,
use_locking=False,
name='ProximalAdagrad'
)
training_op = optimizer.minimize(loss, name='minimize')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet / Input
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
X = np.reshape([ 500 ], (1))
Y = np.reshape([ 15 ], (1, 1))
history = [ ]
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training / Optimize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
first_value = 500
second_value = 15
for i in range(1000):
global_step = global_step + 1
train_loss, temp = sess.run([loss, training_op], feed_dict={inputs:X, labels:Y})
history.append(train_loss)
print( 'steps: ' + str(i) )
a = tf.constant([ first_value ])
b = tf.constant([ second_value ])
v = sess.run( [a, b] )
print( v )
first_value = first_value - i
second_value = second_value - i
sess.close()
print( history )
plt.plot(history)
plt.show()
plt.close()
input('...')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.