---
title: "TensorFlow训练模型报错——莫烦学习例子3"
author: "董瑞龙"
date: "2017-07-20T15:38:50Z"
tags: []
categories: ["互联网"]
slug: "tensorflow-3"
draft: false
lastmod: "2017-07-20T15:38:50Z"
id: 2386155
url: "https://rylan.iteye.com/blog/2386155"
---

运行莫烦TensorFlow教程报错，例子3
https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/3-2-create-NN/

错误：

```python
InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(300, 1), b.shape=(1, 10), m=300, n=10, k=1
	 [[node: matmul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/gpu:0"](_arg_Placeholder_0_0/_7, Variable/read)]]
```


日志报错：
failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED

后查询资料
<https://segmentfault.com/a/1190000009954640?utm_source=itdadao&utm_medium=referral>

解决方案：
如果你是使用 GPU 版 TensorFlow 的话，并且你想在显卡高占用率的情况下（比如玩游戏）训练模型，那你要注意在初始化 Session 的时候为其分配固定数量的显存，否则可能会在开始训练的时候直接报错退出：
这时你需要用下面的方法创建 Session：

```python
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
```