无法将列表转换为张量:ValueError:无法将NumPy数组转换为张量(不支持的对象类型列表)

2024-04-19 22:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的需要你的帮助,我一直在想办法,但还是没有办法解决这个问题。。我可以读取数据集(如图片,使用熊猫),但无法将其转换为张量,尝试了许多不同的方法将列表转换为张量,或数组,矩阵..,但均无效。。 您知道如何更改csv读数以使数据适合tensodflow吗


import sys
import numpy as np
from numpy import array
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from scipy.sparse import csr_matrix
import re
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

from pandas import Series
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from numpy import array
from keras.models import load_model
from sklearn import datasets

df = pd.read_csv('set1.csv') 


#conversão lista em tensor
def my_func(arg):
    arg = tf.convert_to_tensor(arg, dtype=tf.float32)
    return arg

#Uma vgez, e a lista STR é convertida em lista
s1 = df["sensor1"].apply(eval) 
s2 = df["sensor2"].apply(eval) 
s3 = df["sensor3"].apply(eval) 


X = (s1, s2, s3)

X = np.matrix(X)

X = X.transpose()


Y = pd.get_dummies(data=df.categoria).values
#print("shape Y:",(Y.shape), " tipo Y:" ,(type(Y)), "len Y:", len(Y))


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.1, random_state = 42)


print("Train Shape:", X_train.shape,Y_train.shape)
print("Test Shape:", X_test.shape,Y_test.shape)

def model():
    
    if pre_trained_wv is True:
        print("USE PRE TRAINED")
        #num_words = min(max_features, len(word_index) + 1)
        #num_words
        weights_embedding_matrix = load_pre_trained_wv(word_index, num_words, word_embedding_dim)
        input_shape = (max_sequence_length,)
        model_input = Input(shape=input_shape, name="input", dtype='float64')    
        embedding = Embedding(
            num_words, 
            word_embedding_dim,
            input_length=max_sequence_length, 
            name="embedding", 
            weights=[weights_embedding_matrix], 
            trainable=False)(model_input)
        #if bilstm is True:
        #    lstm = Bidirectional(LSTM(word_embedding_dim, dropout=0.2, recurrent_dropout=0.2, name="lstm"))(embedding)
        #else:
        lstm = LSTM(word_embedding_dim, dropout=0.2, recurrent_dropout=0.2, name="lstm")(embedding)

    else:
        input_shape = (max_sequence_length,)
        #model_input = Input(shape=input_shape, name="input", dtype='int32')  
        model_input = Input(shape=input_shape, name="input") 

        embedding = Embedding(max_features, embed_dim, input_length=max_sequence_length, name="embedding")(model_input)
        
        #if bilstm is True:
        #    lstm = Bidirectional(LSTM(embed_dim, dropout=0.2, recurrent_dropout=0.2, name="lstm"))(embedding)
        #else:
        lstm = LSTM(embed_dim, dropout=0.2, recurrent_dropout=0.2, name="lstm")(embedding)
    
    model_output = Dense(2, activation='softmax', name="softmax")(lstm)
    model = Model(inputs=model_input, outputs=model_output)
    return model

model = model()

model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics = ['accuracy'])

print(model.summary())

if not os.path.exists('./{}'.format(filename) ):

    hist = model.fit(
        X_train, 
        Y_train, 
        validation_data=(X_test, Y_test),
        epochs=epochs,
        batch_size=batch_size, 
        shuffle=True,
        verbose=1)

    model.save_weights(filename)    


    # Plot
    plt.figure()
    plt.plot(hist.history['loss'], lw=2.0, color='b', label='train')
    plt.plot(hist.history['val_loss'], lw=2.0, color='r', label='val')
    plt.title('Classificador de Gestos')
    plt.xlabel('Epochs')
    plt.ylabel('Cross-Entropy')
    plt.legend(loc='upper right')
    plt.show()

    plt.figure()
    plt.plot(hist.history['acc'], lw=2.0, color='b', label='train')
    plt.plot(hist.history['val_acc'], lw=2.0, color='r', label='val')
    plt.title('Classificador de Gestos')
    plt.xlabel('Epochs')
    plt.ylabel('Acurácia')
    plt.legend(loc='upper left')
    plt.show()

else:
    model.load_weights('./{}'.format(filename) )

scores = model.evaluate(X_test, Y_test, verbose = 0, batch_size = batch_size)
print("Acc: %.2f%%" % (scores[1]*100))

while True:
    sentence = input("input> ")

    if sentence == "exit":
        break

    new_text = pad_sequences(new_text, maxlen=max_sequence_length, dtype='float64', value=0)
    #new_text = pad_sequences(new_text, maxlen=max_sequence_length, value=0)

    sentiment = model.predict(new_text,batch_size=1,verbose = 2)[0]

    if(np.argmax(sentiment) == 0):
        pred_proba = "%.2f%%" % (sentiment[0]*100)
        print("movimento para direita => ", pred_proba)
    elif (np.argmax(sentiment) == 1):
        pred_proba = "%.2f%%" % (sentiment[1]*100)
        print("movimento para esquerda => ", pred_proba)

这是csv文件:

CSV