当前位置: 移动技术网 > IT编程>脚本编程>Python > 深度学习 图像生成标题

深度学习 图像生成标题

2020年07月24日  | 移动技术网IT编程  | 我要评论

图像生成标题是RNN中的1:N模型
在这里插入图片描述

原理:
训练过程:

  1. 输入一张图片,获得每张图片的逻辑值
  2. 将每张图片的逻辑值和输入的标签值进行交叉熵操作得到损失值
  3. 梯度下降更新模板,使训练更加的吻合

代码实现(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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网