简体   繁体   中英

UnimplementedError: Cast string to float is not supported in model.fit()

Cast string to float is not supported. Trying to use model.fit() bit throws me an error. Should have possibly resulted in processing the data batch wise.

code:

X_train, X_test , y_train , y_test  = train_test_split(data, labels ,test_size = 0.3, random_state  = 1 )
print(X_train.shape , y_train.shape , X_test.shape , y_test.shape)

model = Sequential()
model.add(Conv2D(filters=32, kernel_size = (5,5), activation= 'relu', input_shape = X_train.shape[1:]))
model.add(Conv2D(filters=32, kernel_size = (5,5), activation= 'relu'))
model.add(MaxPool2D(pool_size= (2,2)))
model.add(Dropout(rate= 0.25))
model.add(Conv2D(filters=64, kernel_size = (3,3), activation= 'relu'))
model.add(Conv2D(filters = 64, kernel_size = (3,3) , activation = 'relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(rate=0.25))
model.add(Flatten())
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(rate = 0.5)  )
model.add(Dense(43, activation = 'softmax'))
model.compile(loss= 'categorical_crossentropy', optimizer = 'adam' , metrics = ['accuracy'])

epochs = 15 
Net = model.fit(X_train , y_train , batch_size = 32 , epochs = epochs , validation_data = (X_test , y_test))

error:

UnimplementedError                        Traceback (most recent call last)

   1 epochs = 15
   ----> 2 Net = model.fit(X_train , y_train , batch_size = 32 , epochs = epochs , validation_data = (X_test , y_test))

UnimplementedError:  Cast string to float is not supported
 [[node categorical_crossentropy/Cast (defined at <ipython-input-15-59f0001da0f5>:2) ]] [Op:__inference_train_function_1398]

Function call stack: train_function

      

I had the same problem with the labels.

with open("/input/train.csv") as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader)
    for item in reader:
        training_sentences.append(item[3])
        training_labels.append(item[4])

This piece of code returned the training_labels as

['1',
 '1',
 '1',
 '1',
 '1',
 '1',
 '1',
 '0',
 '0',
 '0',
 ...]

When I compiled the model. I faced the same issue.

UnimplementedError:  Cast string to float is not supported

Here is how I solved it.

with open("/input/train.csv") as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader)
    for item in reader:
        training_sentences.append(item[3])
        this_label = item[4]
        if this_label == '0':
            training_labels.append(0)
        else: 
            training_labels.append(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM