在Anaconda下安装tensorflow 2.0 (超级简单,不用pip,不用额外的安装cuda、cudnn!)

我的安装历程:

  1. 安装了tensorflow 2.0 CPU
  2. CPU运行了一个CNN之后感觉要炸了
  3. 新建环境安装 tensorflow 2.0 GPU版本
  4. 运行了一个程序之后发现我的显卡驱动和cuda版本不匹配
  5. 查看 cuda是10.0版本,而我的 930m 仅支持9.0
  6. 更换 cuda 为 9.0 同时它把我的 tensorflow 2.0 也更改为了 1.14, 好吧
  7. 运行了一个 CNN ,时间真的缩短了很多!
  8. 查看任务管理器,GPU确实有占用

在这里插入图片描述

打开 Anaconda Navigator

新建一个环境,搜索tensorflow, 选择想要安装的版本,apply,等待安装就好了

安装CPU版本和GPU版本都可以直接安装,GPU版本不需要提前安装CUDA等,因为tensorflow-gpu在安装的过程中会自动安装相应版本的CUDA

在这里插入图片描述

sorry,图片上写错了,安装不同版本不是右键,而是单击那个绿色对号,选择需要的版本,具体可以看下面这张图

安装成功之后,点击tensorflow-gpu旁边的绿色三角可以运行terminal

或者可以继续在这里搜索Jupyter notebook,apply,等待安装,然后就可以在绿色三角那里选择jupyter notebook 运行你的程序了~

如果像我一样运行了之后显示cuda版本不对:
在这里插入图片描述

在这里更改版本,它也会相应的将你的tensorflow版本更改的~

Tips

  1. 查看 CUDA Toolkit 和 Compatible Driver Versions

在这里插入图片描述

  1. 一个简单的程序供测试
import numpy as np
import tensorflow as tf
from tensorflow import keras

model = keras.Sequential([keras.layers.Dense(units = 1, input_shape = [1])])

model.compile(optimizer='sgd', loss='mean_squared_error')

xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)

model.fit(xs, ys, epochs=500)

print(model.predict([10.0]))
  1. CNN程序供测试
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels),(test_images, test_labels) = fashion_mnist.load_data()

# 如果加载不出来,可以下载fishion_mnist数据集到本地然后这样:
#=======================================
import numpy as np
import os
import gzip

def load_data(data_folder):

  files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
  ]

  paths = []
  for fname in files:
    paths.append(os.path.join(data_folder,fname))

  with gzip.open(paths[0], 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[1], 'rb') as imgpath:
    x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

  with gzip.open(paths[2], 'rb') as lbpath:
    y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[3], 'rb') as imgpath:
    x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)

  return (x_train, y_train), (x_test, y_test)

(train_images, train_labels), (test_images, test_labels) = load_data('fashion')

#===============================

model = keras.Sequential([
    keras.layers.Flatten(input_shape = (28, 28)),
    keras.layers.Dense(128, activation = tf.nn.relu),
    keras.layers.Dense(10, activation = tf.nn.softmax)
])

train_images = train_images / 255.0
test_images = test_images / 255.0

model.compile(optimizer = 'adam',
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5)

model.evaluate(test_images, test_labels)

我有上传这个的ipynb文件,设置的 0 C币 ,em所以应该可以免费下载叭

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值