Я новичок в тензорном потоке и сверточных нейронных сетях, и я хотел бы построить ИИ, который научится находить режим чисел с плавающей запятой. Но всякий раз, когда я пытаюсь запустить код, я сталкиваюсь с некоторыми ошибками. Вот мой код:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense
train_data = [
[0.5, 0.2, 0.2],
[0.3, 0.3, 0.4],
[0.4, 0.4, 0.5],
[0.8, 0.8, 0.1]
]
train_labels = [
2.0,
3.0,
4.0,
8.0
]
test_data = [
[0.2, 0.5, 0.2],
[0.7, 0.1, 0.7],
[0.6, 0.8, 0.8]
]
test_labels = [
2,
7,
8
]
model = keras.Sequential()
model.add(Dense(4, activation=tf.nn.relu, input_shape=(1,)))
model.add(Dense(2, activation=tf.nn.relu))
model.add(Dense(1, activation=tf.nn.softmax))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.summary()
EPOCHS = 2
BATCH_SIZE=1
model.fit(train_data, train_labels, epochs=EPOCHS, batch_size=BATCH_SIZE)
Однако, когда я пытаюсь запустить код, я получаю следующие ошибки:
Traceback (most recent call last):
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 511, in _apply_op_helper
preferred_dtype=default_dtype)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1175, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 977, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("metrics/acc/Cast_6:0", shape=(?, 1), dtype=float32)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "testNeural.py", line 38, in <module>
metrics=['accuracy'])
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 442, in _method_wrapper
method(self, *args, **kwargs)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 499, in compile
sample_weights=self.sample_weights)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1844, in _handle_metrics
return_stateful_result=return_stateful_result))
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1801, in _handle_per_output_metrics
metric_result = _call_stateless_fn(metric_fn)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1777, in _call_stateless_fn
return weighted_metric_fn(y_true, y_pred, weights=weights, mask=mask)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 647, in weighted
score_array = fn(y_true, y_pred)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\keras\metrics.py", line 1533, in binary_accuracy
return K.mean(math_ops.equal(y_true, y_pred), axis=-1)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 3093, in equal
"Equal", x=x, y=y, name=name)
File "C:\Users\User\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 547, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Equal' Op has type float32 that does not match type int32 of argument 'x'.
Кто-нибудь знает, как это исправить?