这种问题是,对于每一个变量 variable 由于是基于protobuf存在这大小限制(2G),这个时候,我们需要将embedding拆开,拆分成N等分,来使得每一个

variable都在2G以下;

  

 # !/usr/bin/env/python
# coding=utf-8
import tensorflow as tf
import numpy as np input_ids = tf.placeholder(dtype=tf.int32, shape=[None,None]) num_shards = 3
weights = []
weights_shape = np.arange(27).reshape(9, 3)
# assert weights_shape[0] % num_shards == 0
num_shards_len = (weights_shape.shape[0]) / num_shards
assert (weights_shape.shape[0]) % num_shards ==0
begin_ = 0
ends_ = num_shards_len
for i in range(0, num_shards):
if (i + 1) * num_shards_len < weights_shape.shape[0]:
begin_ = i * num_shards_len
if i + 1 == num_shards:
ends_ = weights_shape.shape[0]
else:
ends_ = (i + 1) * num_shards_len
else:
begin_ = i * num_shards_len
ends_ = weights_shape.shape[0]
weights_i = tf.get_variable("words-%02d" % i,
initializer=tf.constant(weights_shape[begin_: ends_, ]))
weights.append(weights_i) input_embedding = tf.nn.embedding_lookup(weights, input_ids,partition_strategy="div") sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
print(sess.run(weights)) print(sess.run(input_embedding, feed_dict={input_ids: [[1, 2], [3, 0], [8, 2], [5, 1]]}))

结果为:

[array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]), array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]), array([[18, 19, 20],
[21, 22, 23],
[24, 25, 26]])]
[[[ 3 4 5]
[ 6 7 8]] [[ 9 10 11]
[ 0 1 2]] [[24 25 26]
[ 6 7 8]] [[15 16 17]
[ 3 4 5]]]

最新文章

  1. 读《JavaScript DOM编程艺术》
  2. Odoo Web Service API
  3. genymotion和eclipse连接问题,一直出错
  4. [LeetCode] Gas Station
  5. codeforces480E Parking Lot
  6. 集成 AliPay - 支付宝
  7. IAR Embedded Workbench IDE 显示行号
  8. Install_ruby
  9. SQL Server 2012数据导入SQL Server 2008
  10. java内存模型分析2
  11. Java的split方法说明
  12. Hibernate 环境搭建
  13. Foreign Exchange(交换生换位置)
  14. 简单的javascript抽奖程序
  15. CTRL key
  16. Java学习--泛型
  17. SpringMVC Controller单例和多例
  18. opencv3 图像处理(一)图像缩放( python与c++ 实现)
  19. App里面如何正确显示用户头像
  20. [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据

热门文章

  1. 爬虫3 requests之json 把json数据转化为字典
  2. TF:TF之Tensorboard实践:将神经网络Tensorboard形式得到events.out.tfevents文件+dos内运行该文件本地服务器输出到网页可视化—Jason niu
  3. 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap
  4. POJ 2481 Cows 【树状数组】
  5. POJ 1733 Parity game 【带权并查集】+【离散化】
  6. java设计模式之-观察者模式(发布-订阅模式)
  7. CDN拾遗
  8. Python爬虫:更加优雅的执行JavaScript(PyV8)
  9. VBA中FIND方法的使用说明zz
  10. SQL Server中变量的声明和使用方法