{"id":289,"hash":"56d1b93c8017d6c7305f8bf47364e8ce95576b31e0c3cbfd5db277371b42048e","pattern":"Tensorflow - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)","full_message":"Continuation from previous question: Tensorflow - TypeError: 'int' object is not iterable\n\nMy training data is a list of lists each comprised of 1000 floats. For example, x_train[0] =\n\n[0.0, 0.0, 0.1, 0.25, 0.5, ...]\n\nHere is my model:\n\nmodel = Sequential()\n\nmodel.add(LSTM(128, activation='relu',\n               input_shape=(1000, 1), return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(128, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1, activation='sigmoid'))\n\nopt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)\n\nmodel.compile(optimizer='rmsprop',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))\n\nHere is the error I'm getting:\n\nTraceback (most recent call last):\n      File \"C:\\Users\\bencu\\Desktop\\ProjectFiles\\Code\\Program.py\", line 88, in FitModel\n        model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training.py\", line 728, in fit\n        use_multiprocessing=use_multiprocessing)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training_v2.py\", line 224, in fit\n        distribution_strategy=strategy)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training_v2.py\", line 547, in _process_training_inputs\n        use_multiprocessing=use_multiprocessing)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training_v2.py\", line 606, in _process_inputs\n        use_multiprocessing=use_multiprocessing)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\data_adapter.py\", line 479, in __init__\n        batch_size=batch_size, shuffle=shuffle, **kwargs)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\data_adapter.py\", line 321, in __init__\n        dataset_ops.DatasetV2.from_tensors(inputs).repeat()\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\data\\ops\\dataset_ops.py\", line 414, in from_tensors\n        return TensorDataset(tensors)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\data\\ops\\dataset_ops.py\", line 2335, in __init__\n        element = structure.normalize_element(element)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\data\\util\\structure.py\", line 111, in normalize_element\n        ops.convert_to_tensor(t, name=\"component_%d\" % i))\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\ops.py\", line 1184, in convert_to_tensor\n        return convert_to_tensor_v2(value, dtype, preferred_dtype, name)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\ops.py\", line 1242, in convert_to_tensor_v2\n        as_ref=False)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\ops.py\", line 1296, in internal_convert_to_tensor\n        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\tensor_conversion_registry.py\", line 52, in _default_conversion_function\n        return constant_op.constant(value, dtype, name=name)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\constant_op.py\", line 227, in constant\n        allow_broadcast=True)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\constant_op.py\", line 235, in _constant_impl\n        t = convert_to_eager_tensor(value, ctx, dtype)\n      File \"C:\\Users\\bencu\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\tensorflow_core\\python\\framework\\constant_op.py\", line 96, in convert_to_eager_tensor\n        return ops.EagerTensor(value, ctx.device_name, dtype)\n    ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).\n\nI've tried googling the error myself, I found something about using the tf.convert_to_tensor function. I tried passing my training and testing lists through this but the function won't take them.","ecosystem":"pypi","package_name":"tensorflow","package_version":null,"solution":"TL;DR Several possible errors, most fixed with x = np.asarray(x).astype('float32').\n\nOthers may be faulty data preprocessing; ensure everything is properly formatted (categoricals, nans, strings, etc). Below shows what the model expects:\n\n[print(i.shape, i.dtype) for i in model.inputs]\n[print(o.shape, o.dtype) for o in model.outputs]\n[print(l.name, l.input_shape, l.dtype) for l in model.layers]\n\nThe problem's rooted in using lists as inputs, as opposed to Numpy arrays; Keras/TF doesn't support former. A simple conversion is: x_array = np.asarray(x_list).\n\nThe next step's to ensure data is fed in expected format; for LSTM, that'd be a 3D tensor with dimensions (batch_size, timesteps, features) - or equivalently, (num_samples, timesteps, channels). Lastly, as a debug pro-tip, print ALL the shapes for your data. Code accomplishing all of the above, below:\n\nSequences = np.asarray(Sequences)\nTargets   = np.asarray(Targets)\nshow_shapes()\n\nSequences = np.expand_dims(Sequences, -1)\nTargets   = np.expand_dims(Targets, -1)\nshow_shapes()\n\n# OUTPUTS\nExpected: (num_samples, timesteps, channels)\nSequences: (200, 1000)\nTargets:   (200,)\n\nExpected: (num_samples, timesteps, channels)\nSequences: (200, 1000, 1)\nTargets:   (200, 1)\n\nAs a bonus tip, I notice you're running via main(), so your IDE probably lacks a Jupyter-like cell-based execution; I strongly recommend the Spyder IDE. It's as simple as adding # In[], and pressing Ctrl + Enter below:\n\nFunction used:\n\ndef show_shapes(): # can make yours to take inputs; this'll use local variable values\n    print(\"Expected: (num_samples, timesteps, channels)\")\n    print(\"Sequences: {}\".format(Sequences.shape))\n    print(\"Targets:   {}\".format(Targets.shape))","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/58636087/tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte","votes":168,"created_at":"2026-04-19T04:41:46.110870+00:00","updated_at":"2026-04-19T04:51:57.732928+00:00"}