欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow矩阵过大问题的解决

发布时间:2025/4/16 编程问答 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Tensorflow矩阵过大问题的解决 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

问题:推荐系统中,用户和物品矩阵进行embedding,但矩阵过大时,超过2G时,会有如下提示

ValueError: Cannot create a tensor proto whose content is larger than 2GB.

出现问题的代码语句是:

self.user_item_embedding = tf.convert_to_tensor(matrix)

matrix是一个numpy的大型矩阵。

解决:

 matrix_init = tf.placeholder(tf.float32, shape=(self.shape[0], self.shape[1]))
 matrixV = tf.Variable(matrix_init)
self.user_item_embedding = tf.convert_to_tensor(matrixV)

先构建占位符,在设置变量,并在sess.run时赋予:

elf.sess.run(tf.global_variables_initializer(), feed_dict={matrix_init: matrix})

参考:

https://stackoverflow.com/questions/51470991/create-a-tensor-proto-whose-content-is-larger-than-2gb

https://stackoverflow.com/questions/35394103/initializing-tensorflow-variable-with-an-array-larger-than-2gb

总结

以上是生活随笔为你收集整理的Tensorflow矩阵过大问题的解决的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。