在张量流中训练CNN模型时出现未实现的错误

时间:2020-10-23 16:46:44

标签: python tensorflow machine-learning conv-neural-network

我正在尝试训练CNN模型。

要分析的图像大小为(3200,149,1)。

数据采用以下格式:

enter image description here

L1,L2,L3和L4是第一,第二,第三和第四样本的标签。

用于训练模型的代码:

import tensorflow as tf
from general import cwtVector
import numpy as np
def generate_model():
    model = tf.keras.Sequential([
    # first convolutional layer
    tf.keras.layers.Conv2D(8, (3, 3), strides=(1, 1), activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
    
    # second convolutional layer
    tf.keras.layers.Conv2D(16, (3, 3), strides=(1, 1), activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
    
    #fully connected classifier
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(6, activation='softmax') #6 outputs
    ])
    return model
mod = generate_model()
train_images = []
test_images = []
train_labels = []
test_labels = []
for i in range(40,60):
    train_images.append(cwtVector[i][1])
    train_labels.append(cwtVector[i][0])
for i in range(150,160):
    test_images.append(cwtVector[i][1])
    test_labels.append(cwtVector[i][0])
    
#conversion to np.array to be fed into the model.fit
train_images = np.array(train_images)
test_images = np.array(test_images)
train_labels = np.array(train_labels)
test_labels = np.array(test_labels)
mod.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = mod.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))

,其中cwtVector是数据列表,如上图所示(例如cwtVector[0][0]是第一个样本的标签,cwtVector[0][1]是数据)。但是,当我运行脚本时,出现此错误:

    Reloaded modules: general
Train on 20 samples, validate on 10 samples
Epoch 1/10
20/20 [==============================] - 0s 18ms/sample
Traceback (most recent call last):

  File "C:\Users\Sina\Desktop\EEG Code\CNN.py", line 51, in <module>
    validation_data=(test_images, test_labels))

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 342, in fit
    total_epochs=epochs)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
    batch_outs = execution_function(iterator)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
    distributed_function(input_fn))

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
    result = self._call(*args, **kwds)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 632, in _call
    return self._stateless_fn(*args, **kwds)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 2363, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_call
    self.captured_inputs)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 1692, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\function.py", line 545, in call
    ctx=ctx)

  File "C:\Users\Sina\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)

  File "<string>", line 3, in raise_from

UnimplementedError:  Cast string to int64 is not supported
     [[node loss/output_1_loss/Cast (defined at C:\Users\Sina\Desktop\EEG Code\CNN.py:51) ]] [Op:__inference_distributed_function_4313]

Function call stack:
distributed_function



2020-10-23 17:00:25.490370: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-10-23 17:00:26.127265: W tensorflow/core/framework/op_kernel.cc:1632] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to int64 is not supported
2020-10-23 17:00:26.127333: E tensorflow/core/common_runtime/executor.cc:654] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
     [[{{node loss/output_1_loss/Cast}}]]
2020-10-23 17:21:33.256461: W tensorflow/core/framework/op_kernel.cc:1632] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to int64 is not supported
2020-10-23 17:21:33.258812: E tensorflow/core/common_runtime/executor.cc:654] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
     [[{{node loss/output_1_loss/Cast}}]]
2020-10-23 17:23:10.600981: W tensorflow/core/framework/op_kernel.cc:1632] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to int64 is not supported
2020-10-23 17:23:10.601035: E tensorflow/core/common_runtime/executor.cc:654] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
     [[{{node loss/output_1_loss/Cast}}]]
2020-10-23 17:25:26.461077: W tensorflow/core/framework/op_kernel.cc:1632] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to int64 is not supported
2020-10-23 17:25:26.461118: E tensorflow/core/common_runtime/executor.cc:654] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
     [[{{node loss/output_1_loss/Cast}}]]
2020-10-23 17:26:49.458059: W tensorflow/core/framework/op_kernel.cc:1632] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to int64 is not supported
2020-10-23 17:26:49.458108: E tensorflow/core/common_runtime/executor.cc:654] Executor failed to create kernel. Unimplemented: Cast string to int64 is not supported
     [[{{node loss/output_1_loss/Cast}}]]

我是否可以假设这是因为图像数据中的一个单元格已保存为字符串? 甚至似乎问题只在进行评估,因为训练位似乎已经完成,没有问题,这与validation_data函数有关吗?

PS:我知道训练数据太小了,我有一个更大的数据集,只是用来检查脚本是否有效。

0 个答案:

没有答案