Ошибка типа: ожидаемая двоичная или Unicode строка Ошибка Tensorflow - PullRequest
0 голосов
/ 21 марта 2020

В моем коде я загрузил набор данных, затем я создал свои переменные и фрейм данных, а затем создал свою модель для тестирования. Во время обучения моей модели я получаю эту ошибку, я не знаю почему.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/util/structure.py in normalize_element(element)
     92       try:
---> 93         spec = type_spec_from_value(t, use_fallback=False)
     94       except TypeError:

16 frames
TypeError: Could not build a TypeSpec for 196      14
2463    248
731     314
1147    161
1972    125
       ... 
763     318
835      32
1653     28
2607    165
2732    198
Name: Adult_Mortality, Length: 2645, dtype: object with type Series

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
tensorflow/python/framework/fast_tensor_util.pyx in tensorflow.python.framework.fast_tensor_util.AppendObjectArrayToTensorProto()

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/compat.py in as_bytes(bytes_or_text, encoding)
     85   else:
     86     raise TypeError('Expected binary or unicode string, got %r' %
---> 87                     (bytes_or_text,))
     88 
     89 

TypeError: Expected binary or unicode string, got 14.0

Мой код очень прост, и я написал код в Google Collaboratory:

%tensorflow_version 2.x 

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
from sklearn.model_selection import train_test_split

import tensorflow.compat.v2.feature_column as fc

import tensorflow as tf

df = pd.read_csv('heal.csv', low_memory=False)
dftrain, dfeval = train_test_split(df, test_size = 0.1, random_state = 0)
dftrain.fillna('0', inplace=True)
y_train = dftrain.pop('Life_expectancy')
y_eval = dfeval.pop('Life_expectancy')
print(y_train)
dftrain.head()
dfeval.shape

CATEGORICAL_COLUMNS = ['Country']
NUMERIC_COLUMNS = ['Year', 'Status', 'Adult_Mortality', 'Infant_Deaths', 'Alcohol',
                   'Percentage_Expenditure', 'Hepatitis_B', 'Measles', 'BMI',
                   'Under_five_deaths', 'Polio', 'Total_expenditure', 'Diphtheria',
                   'HIV/AIDS', 'GDP', 'Population', 'thinnes_1-19_years', 'thinness_5-9_years',
                   'Income_composition_of_resources', 'Schooling']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  vocabulary = dftrain[feature_name].unique()  # gets a list of all unique values from given feature column
  feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

print(feature_columns)

def make_input_fn(data_df, label_df, num_epochs=15, shuffle=True, batch_size=32):
  def input_function():  # inner function, this will be returned
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))  # create tf.data.Dataset object with data and its label
    if shuffle:
      ds = ds.shuffle(1000)  # randomize order of data
    ds = ds.batch(batch_size).repeat(num_epochs)  # split dataset into batches of 32 and repeat process for number of epochs
    return ds  # return a batch of the dataset
  return input_function  # return a function object for use

train_input_fn = make_input_fn(dftrain, y_train)  # here we will call the input_function that was returned to us to get a dataset object we can feed to the model
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)

linear_est.train(train_input_fn)  # train
result = linear_est.evaluate(eval_input_fn)  # get model metrics/stats by testing on tetsing data

clear_output()  # clears console output
print(result['accuracy'])  # the result variable is simply a dict of stats about our model
print(result)

Любая помощь будет очень заметна. Пожалуйста, если вы сможете описать проблему, было бы здорово.

...