我试图在 Keras 上以双精度运行 LeNet,但它失败并出现错误:TypeError: Input 'filter' of 'Conv2D' Op has type float64 that does not match type float32 of argument 'input'.. 我使用的代码如下:
import numpy as np
from sklearn.utils import shuffle
import keras
from keras.models import Sequential
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout,Flatten
from keras import backend as K
from keras.models import Model
from keras.utils import np_utils
import time
import tensorflow as tf
K.set_floatx('float64') # Note: the code works if we comment this line, i.e., with single precision
from mlxtend.data import mnist_data
X, y = mnist_data()
X = X.astype(np.float64)
X, y = shuffle(X, y)
keras_model = Sequential()
keras_model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(28,28,1), padding='same'))
keras_model.add(MaxPooling2D(pool_size=(2, 2)))
keras_model.add(Conv2D(64, (5, 5), activation='relu', padding='same'))
keras_model.add(MaxPooling2D(pool_size=(2, 2)))
keras_model.add(Flatten())
keras_model.add(Dense(512, activation='relu'))
keras_model.add(Dropout(0.5))
keras_model.add(Dense(10, activation='softmax'))
keras_model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.95, decay=5e-4, nesterov=True))
keras_model.fit(X.reshape((-1, 28,28, 1)), np_utils.to_categorical(y, 10), epochs=1, batch_size=64)
Run Code Online (Sandbox Code Playgroud)
任何建议都非常感谢:)
您有一个游戏 NVIDIA GPU。您只能使用float32或int32,仅此而已。
这是 TensorFlow 的默认设置。
由于 Nvidia 具有 CUDA 功能的 GPU 的限制,Tensorflow 引入了此默认设置。我在这里找到的最好的解释。因此,高级 Tesla GPU 在float16和float64上也能很好地工作,但游戏 GPU 只能在或上工作float32,并且性能非常差。
我认为我们都在关注由更昂贵的 AMD GPU 支持的 OpenCL。不幸的是,目前 TensorFlow 还不支持 OpenCL。 float16float64
建议#1:你被 float32 困住了。拥有该硬件时忘记更改它。
建议#2:一旦您获得了适合 float16 的 GPU,请更改为该值。机器学习不需要高于此的高精度。
| 归档时间: |
|
| 查看次数: |
2669 次 |
| 最近记录: |