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

    1.CSS a.定义:CSS 指层叠样式表 b.意义:为了解决内容与表现分离的问题 c.特点:多个样式可层叠为一 2.用法: a.行内样式:style <span style="col ...

  2. HDU 1281 棋盘游戏 (枚举+最大匹配)

    <题目链接> Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单 ...

  3. POJ 3104 Drying (经典)【二分答案】

    <题目链接> 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服没分钟可以自动蒸发掉一滴水,用烘干机烘衣服时不蒸发.问最少需要多少 ...

  4. Git 使用问题记录

    问题一:新文件 add 后,提示有 modified 内容 描述:在 master 分支中增加了一个新的文件夹(-/cb4.6-zh),但执行 git add 后再查看状态却提示有 modified ...

  5. 765. 有效的三角形.md

    描述 给出三个整数 a, b, c, 如果它们可以构成三角形,返回 true. 三角形的定义 (Wikipedia) 样例 给定 a = 2, b = 3, c = 4 返回 true 给定 a = ...

  6. 手机端input获取焦点弹出键盘时挡住input解决方案

    问题重现 原始页面:页面中有header.main.footer三部分,其中 header 和 footer 通过 position: fixed; 定位在浏览器顶部和底部. 其中,footer 中有 ...

  7. BZOJ.4650.[NOI2016]优秀的拆分(后缀数组 思路)

    BZOJ 洛谷 令\(st[i]\)表示以\(i\)为开头有多少个\(AA\)这样的子串,\(ed[i]\)表示以\(i\)结尾有多少个\(AA\)这样的子串.那么\(Ans=\sum_{i=1}^{ ...

  8. RabbitMQ主题模式

    Send类 package topics; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; imp ...

  9. python基础一 ------简单队列用作历史记录

    #需求:测试历史记录,一个猜字游戏,能在重新进入游戏时查看输入历史# #-*-coding:utf-8-*- from random import randint from collections i ...

  10. 【PYTHON】 Missing parentheses in call to 'print'

    Microsoft Windows [版本 10.0.15063] (c) 2017 Microsoft Corporation.保留所有权利. C:\Users\Jam>python Pyth ...