图像生成标题是RNN中的1:N模型
原理:
训练过程:
代码实现(Tensors类)
import tensorflow as tf
class MySubTensors:
def __init__(self):
self.x = tf.placeholder(tf.float64, [None, 224, 224, 3], 'x'] # 输入图片
self.y = tf.placeholder(tf.ini64, [None, 50], 'y') # 输入标题
x = inception(self.x, name='inception') # 使用inception神经网络
x = tf.layers.flatten(x) # 对卷积后的x进行拍平操作
x = tf.nn.dropout(x, 0.6) # 使用dropout防止过拟合
x = tf.layers.dense(x, 200, name="dense1") # 对x的数据进行dense操作,获得语义值,语义值的维度是[-1, 200]
y = tf.one_hot(self.y, 4340) # 将标签值做one_hot操作,方便计算交叉熵
cell1 = tf.nn.rnn_cell.LSTMCell(200, name="cell1") # 使用rnn神经网络
cell2 = tf.nn.rnn_cell.LSTMCell(200, name="cell2")
cell = tf.nn.rnn_cell.MultiRNNCell([cell1, cell2])
state = cell.zero_state(tf.shape(x)[0], x.dtype)
losses = [] # 定义损失,将损失值添加到损失列表中
for i in range(50): # 获得50个字的语义
yi_predice, state = cell(x, state)
yi_predice = tf.layers.dense(yi_predice, 4340, name="dense2")
tf.get_variable_scope().reuse_variables()
lossi = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y[:,i,:], logits=yi_predice)
losses.append(lossi)
loss = tf.reduce_mean(losses) #汇总损失值
本文地址:https://blog.csdn.net/qq_38973721/article/details/107517570
如对本文有疑问, 点击进行留言回复!!
牛客多校第三场 A-Clam and Fish【贪心】+ B-Classical String Problem【思维】
网友评论