这种问题是,对于每一个变量 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]]]

tensorflow 在加载大型的embedding模型参数时,会遇到cannot be larger than 2GB的更多相关文章

  1. Tensorflow同时加载使用多个模型

    在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...

  2. MFC加载大型osg模型

    MFC加载模型,发现打开 Navid 缓冲等选项后,加载大型模型的速度就快了很多. #include "stdafx.h" #include "OSGObject.h&q ...

  3. [CG从零开始] 6. 加载一个柴犬模型学习UV贴图

    在第 5 篇文章中,我们成功加载了 fbx 模型,并且做了 MVP 变换,将立方体按照透视投影渲染了出来.但是当时只是随机给顶点颜色,并且默认 fbx 文件里只有一个 mesh,这次我们来加载一个柴犬 ...

  4. "C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE 浏览器打开时全屏模式

    "C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE浏览器打开时全屏模式

  5. tensorflow数据加载、模型训练及预测

    数据集 DNN 依赖于大量的数据.可以收集或生成数据,也可以使用可用的标准数据集.TensorFlow 支持三种主要的读取数据的方法,可以在不同的数据集中使用:本教程中用来训练建立模型的一些数据集介绍 ...

  6. tensorflow学习笔记2:c++程序静态链接tensorflow库加载模型文件

    首先需要搞定tensorflow c++库,搜了一遍没有找到现成的包,于是下载tensorflow的源码开始编译: tensorflow的contrib中有一个makefile项目,极大的简化的接下来 ...

  7. 用TWaver加载大型游戏场景一例

    游戏中经常会出现一些大型的户外场景,例如一个小镇.一座古城等.通常这种场景中包含了较多的建筑.道路.桥梁等等元素,其3D模型比较大且复杂.在使用TWaver加载时,可使用一些技巧,让加载速度更快.显示 ...

  8. tensorflow数据集加载

    本篇涉及的内容主要有小型常用的经典数据集的加载步骤,tensorflow提供了如下接口:keras.datasets.tf.data.Dataset.from_tensor_slices(shuffl ...

  9. Windows下pycharm远程连接服务器调试-tensorflow无法加载问题

    最近打算在win系统下使用pycharm开发程序,并远程连接服务器调试程序,其中在import tensorflow时报错如图所示(在远程服务器中执行程序正常): 直观错误为: ImportError ...

随机推荐

  1. Idea中lombok不生效原因

    我们可以通过在maven中插入配置信息 <dependency> <groupId>org.projectlombok</groupId> <artifact ...

  2. 找bug hhh

    http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4434 没有用队列,疯狂找不到bug,后来发现很简单的判断时==n和m了,本来心花怒放,测试 ...

  3. ECMAScript6 入门 函数的扩展

    为函数参数设定默认值 function log(x, y = 'World') { console.log(x, y); } log('Hello') // Hello World log('Hell ...

  4. ReentrantLock和condition源码浅析(二)

    转载请注明出处... 接着上一篇的ReentrantLock和condition源码浅析(一),这篇围绕着condition 一.condition的介绍 在这里为了作对比,引入Object类的两个方 ...

  5. 利用nginx,腾讯云免费证书制作https

    之前一直在研究,https怎么弄.最近看到了腾讯云提供的免费得ssl证书,寻思把网站弄成https. 首先先去腾讯云购买一个免费得证书. 点击后填写内容, 然后下载证书 解压证书就可以看到,提供四种方 ...

  6. Vue.Js初学踩坑

    1 Vue2之后取消了v-bind的.sync修饰符,意味着父子组件的数据不能以此来实现双向绑定. 2 Vue2之后取消了filterBy过滤器,所以以下这种用法是错误的了. <tbody> ...

  7. javaweb笔记

    (1)web项目需复制web.xml文件 (2)需要复制classes文件 需要把bin里面的com----复制到classes中

  8. ionic2 隐藏滚动条

    方法 在全局样式,即app.scss里添加样式: ::-webkit-scrollbar { display: none !important; }

  9. python gui 之 tkinter库

    http://blog.csdn.net/jcodeer?viewmode=contents http://tieba.baidu.com/p/3082739560 http://blog.sina. ...

  10. 使用 OpenSessionInViewFilter 配置加载

    1.使用 OpenSessionInViewFilter