VAE是一个神奇得算法,其实思想倒是有点像word2vec,只是在其上加了一层bayesian的思想,这点上又倒是有点像LDA了;

个人觉得,VAE挖掘的好的话,倒是有很大的潜力和应用的,因为它是真正意义上的无监督的,句子表示成向量之后,然后你就可以想干嘛就干嘛了;

简单介绍一下VAE,就是一个变分估算参数后验概率,输入输出都是句子本身;

下面介绍一种最简单的VAE的实现,用1-gram,就是把句子表示成1*vocabulary size的向量;

tensorflow实现的;现在我在开发基于seq2seq的VAE算法,github上面倒是有几个,但是基于tensorflow的没一个写的让我满意的;

#encoding=utf-8
importosimportitertoolsimportnumpy as npimporttensorflow as tffrom reader importTextReaderimportrandom embed_dim= 500h_dim= 100data_path= './n_gram/'model_dir= './n_gram/model_dir/'reader=TextReader(data_path)defcreate_train_op(loss):train_op= tf.contrib.layers.optimize_loss(loss =loss, global_step=tf.contrib.framework.get_global_step(), learning_rate= 0.01, clip_gradients= 10.0, optimizer= "Adam")returntrain_opglobal_step= tf.Variable(0, name = 'global_step', trainable=False)tx=tf.placeholder(tf.int64, [None, reader.vocab_size])
x=tf.to_float(tx)batch_size=tf.placeholder(tf.int64)
w=tf.placeholder(tf.float32)with tf.variable_scope('encoder'):w_1= tf.get_variable('w_1', [reader.vocab_size, embed_dim], initializer =tf.truncated_normal_initializer())b_1= tf.get_variable('b_1', [embed_dim], initializer =tf.truncated_normal_initializer())L1=tf.nn.bias_add(tf.matmul(x, w_1), b_1)L1=tf.nn.tanh(L1)w_2= tf.get_variable('w_2', [embed_dim, embed_dim], initializer =tf.truncated_normal_initializer())b_2= tf.get_variable('b_2', [embed_dim], initializer =tf.truncated_normal_initializer())L2=tf.nn.bias_add(tf.matmul(L1, w_2), b_2)L2=tf.nn.tanh(L2)w_encoder_mu= tf.get_variable('w_encoder_mu', [embed_dim, h_dim], initializer = tf.truncated_normal_initializer(0, 0.01))b_encoder_mu= tf.get_variable('b_encoder_mu', [h_dim], initializer = tf.truncated_normal_initializer(0, 0.001))w_encoder_var= tf.get_variable('w_encoder_var', [embed_dim, h_dim], initializer = tf.truncated_normal_initializer(0, 0.01))b_encoder_var= tf.get_variable('b_encoder_var', [h_dim], initializer = tf.truncated_normal_initializer(0, 0.01))mu=tf.nn.bias_add(tf.matmul(L2, w_encoder_mu), b_encoder_mu)log_sigma_sq=tf.nn.bias_add(tf.matmul(L2, w_encoder_var), b_encoder_var)eps= tf.random_normal([batch_size, h_dim], 0, 1, dtype =tf.float32)sigma=tf.sqrt(tf.exp(log_sigma_sq))h= mu + sigma*epswith tf.variable_scope('decoder') as vs:R= tf.get_variable('R', [h_dim, reader.vocab_size], initializer = tf.truncated_normal_initializer(0, 0.0001))b= tf.get_variable('b', [reader.vocab_size], initializer = tf.truncated_normal_initializer(0, 0.0001))e= -tf.matmul(h, R) +b p_x_i= tf.nn.softmax(e, -1)e_loss= -0.5 * tf.reduce_sum(1.0 + log_sigma_sq - tf.square(mu) - tf.exp(log_sigma_sq), 1)
g_loss= -tf.reduce_sum(tf.log(p_x_i + 1e-10)*x, 1)
g_loss_stand= -tf.log(1.0/tf.reduce_sum(x, 1))*tf.reduce_sum(x, 1)#g_loss = g_loss/tf.maximum(g_loss_stand, 1.0)
e_loss_mean=tf.reduce_mean(e_loss)
g_loss_mean=tf.reduce_mean(g_loss)loss= 0.1*e_loss +g_loss
loss=tf.reduce_mean(loss)encoder_var_list=[]
decoder_var_list=[]for var intf.trainable_variables():if 'encoder' invar.name:encoder_var_list.append(var)elif 'decoder' invar.name:decoder_var_list.append(var)optim_e= tf.train.AdamOptimizer(learning_rate=0.05).minimize(e_loss, global_step=global_step, var_list=encoder_var_list)
optim_g= tf.train.AdamOptimizer(learning_rate=0.05).minimize(g_loss, global_step=global_step, var_list=decoder_var_list)
train_op=create_train_op(loss)saver=tf.train.Saver()
with tf.Session() as sess:sess.run(tf.initialize_all_variables())ckpt=tf.train.get_checkpoint_state(model_dir)if ckpt andckpt.model_checkpoint_path:print 'the model being restored is'printckpt.model_checkpoint_path saver.restore(sess, ckpt.model_checkpoint_path)print 'sucesssfully restored the session'count=global_step.eval()for k inrange(0, 0):data, length=reader.iterator()em, gm, lm, _= sess.run([e_loss_mean, g_loss_mean, loss, train_op], feed_dict ={tx: data, batch_size:length,w:k/1000.0})print 'After\t' + str(global_step.eval()) + 'th step,the loss\t' + str(lm) + '\t kL loss\t' + str(em) + '\tdecoder loss\t' +str(gm)global_step.assign(count).eval()if k%10 ==0:saver.save(sess, model_dir+ 'model.ckpt', global_step =global_step)count+= 1

转载于:https://www.cnblogs.com/LarryGates/p/6565851.html

用VAE(variational autoencoder)做sentence embedding/representation或者其他任何结构数据的热presentation...相关推荐

  1. 详解变分自编码器VAE(Variational Auto-Encoder)

    前言 过去虽然没有细看,但印象里一直觉得变分自编码器(Variational Auto-Encoder,VAE)是个好东西.趁着最近看概率图模型的三分钟热度,我决定也争取把 VAE 搞懂. 于是乎照样 ...

  2. 变分自动编码器(VAE variational autoencoder)

    文章目录 自动编码器 AutoEncoder 变分推断 Variational Inference 变分自动编码器 Variational AutoEncoder 条件变分自动编码器 Conditio ...

  3. VAE(Variational Autoencoder)的原理

    Kingma, Diederik P., and Max Welling. "Auto-encoding variational bayes." arXiv preprint ar ...

  4. 从零点五开始的深度学习笔记——VAE(Variational AutoEncoder) (二)概率角度理解VAE结构

    概率角度理解VAE结构 1. 从联合概率分布构造的损失函数开始的一切 1.1 定义 1.2 推导过程 1.3 损失函数的理解 2. 总结 1. 从联合概率分布构造的损失函数开始的一切 一个优秀的生成网 ...

  5. Variational AutoEncoder(VAE)变分自编码器

    [本文转载自博客]:解析Variational AutoEncoder(VAE): https://www.jianshu.com/p/ffd493e10751 文章目录 1. 模型总览 1.1 Au ...

  6. 从AE(Auto-encoder)到VAE(Variational Auto-Encoder)

    一.Auto-Encoder (AE) 1)Auto-encoder概念 自编码器要做的事:将高维的信息通过encoder压缩到一个低维的code内,然后再使用decoder对其进行重建." ...

  7. 变分自编码器(VAE,Variational Auto-Encoder)

    变分自编码器(Variational auto-encoder,VAE)是一类重要的生成模型(Generative Model) 除了VAEs,还有一类重要的生成模型GANs VAE 跟 GAN 比较 ...

  8. MATLAB实现自编码器(四)——变分自编码器实现图像生成Train Variational Autoencoder (VAE) to Generate Images

    本文是对Train Variational Autoencoder (VAE) to Generate Images网页的翻译,该网页实现了变分自编码的图像生成,以MNIST手写数字为训练数据,生成了 ...

  9. 【DL笔记】Tutorial on Variational AutoEncoder——中英文对照(更新中)

    更新时间:2018-09-25 Abstract In just three years, Variational Autoencoders (VAEs) have emerged as one of ...

  10. 2022年几款前沿的文本语义检索/Sentence Embedding方法:Gradient Cache, SGPT,ART,DPTDR,RocketQAv2, ERNIE-Search等

    最近研究了一些最新的关于搜索方向的论文,发现了几篇有代表性的论文,我这里分享出来,跟大家一起学习共同进步.目前的搜索架构都是召回和排序,召回采用的是BM25,dual-encoder, bi-enco ...

最新文章

  1. ubuntu8.10家庭使用(一)
  2. IT 行业的创新 - 创新的迷思 (5-6)
  3. matlab白噪声模块,matlab白噪声实现
  4. delphi 更改DBGrid 颜色技巧
  5. IT巨头埃森哲遭 LockBit 勒索攻击,黑客威胁泄露数据
  6. python书籍下载网站_Python 精品开源电子书网站
  7. LoadRunner11下载、安装与破解
  8. UCOS操作系统——任务的挂起与恢复(四)
  9. 鸿蒙系统底部任务栏无响应,win10底部任务栏无响应怎么办?win10底部任务栏无响应修复方法汇总...
  10. 在Postfix里给邮箱定虚拟别名
  11. java 求集合平均数_n个数的几何平均数小于等于它的算术平均数,详细证明过程...
  12. DOSBOX怎么使用 从编译到连接到执行操作全过程 + debug
  13. 电脑桌面云便签怎么设置开启邮箱提醒待办事项?
  14. (原创)ics-openvpn编译详解
  15. 【智能制造】中国工程院正式提出新一代智能制造,理论自信引领制造强国
  16. HAL-STM32-USB内部Flash虚拟U盘更新程序
  17. テクニカルアーティストに必要な資料集めようぜ!
  18. 【Echarts】Echarts给title添加背景图片
  19. CSS 文字两行显示,超出隐藏
  20. docker部署finebi 帆软

热门文章

  1. 360n7手机断网没信号_你的手机信号为什么总比别人差?原因没那么简单
  2. css基础知识汇总3
  3. html语言考点,HTML知识点
  4. java获取数据库列的别名_java – JDBC ResultSet获取具有表别名的列
  5. PyTorch载入图片ToTensor,PIL和OpenCV读取图片plt.imread和PIL.Image.open
  6. MatConvnet工具箱文档翻译理解二
  7. Caffe傻瓜系列(1):数据层及参数
  8. python实现两个任意字符串乘积
  9. 64位win10下安装xgboost python包的教程
  10. 面试常问:BIO,NIO,AIO