关于python:Tensorflow – ‘无法将元素作为字节’错误

Tensorflow - 'Unable to get element as bytes' error

以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
import pandas as pd



# DATA PREPARE
df = pd.read_csv('housing.csv')
df = df.dropna()
print(df.head)
print(df.describe())
print(df.info())



# NORMALIZATION
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()
scaler.fit(df[['housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income',
               'median_house_value']])
df_scaled_cols = scaler.transform(df[['housing_median_age', 'total_rooms', 'total_bedrooms',
                                      'population', 'households', 'median_income', 'median_house_value']])
df_scaled_cols = pd.DataFrame(data=df_scaled_cols, columns=['housing_median_age', 'total_rooms', 'total_bedrooms',
                                                            'population', 'households', 'median_income',
                                                            'median_house_value'])

df = pd.concat([df_scaled_cols, df['ocean_proximity']], axis=1)



# DATAFRAME INTO X AND Y -> TRAIN TEST SPLIT
x_data = df[['housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income',
             'ocean_proximity']]
y_label = df['median_house_value']

X_train, X_test, y_train, y_test = train_test_split(x_data, y_label, test_size=0.3)



# FEATURE COLUMNS FROM DATA

m_age = tf.feature_column.numeric_column('housing_median_age')
rooms = tf.feature_column.numeric_column('total_rooms')
bedrooms = tf.feature_column.numeric_column('total_bedrooms')
population = tf.feature_column.numeric_column('population')
households = tf.feature_column.numeric_column('households')
income = tf.feature_column.numeric_column('median_income')
ocean = tf.feature_column.categorical_column_with_hash_bucket('ocean_proximity', hash_bucket_size=10)
embedded_ocean = tf.feature_column.embedding_column(ocean, dimension=4)

feat_cols = [m_age, rooms, bedrooms, population, households, income, embedded_ocean]



# 3 INPUT FUNCTIONS

train_input_func = tf.estimator.inputs.pandas_input_fn(x=X_train, y=y_train, batch_size=10, num_epochs=1000,
                                                       shuffle=True)
test_input_func = tf.estimator.inputs.pandas_input_fn(x=X_test, y=y_test, batch_size=10, num_epochs=1, shuffle=False)
predict_input_func = tf.estimator.inputs.pandas_input_fn(x=X_test, batch_size=10, num_epochs=1, shuffle=False)



# DNN_Reg MODEL

dnn_model = tf.estimator.DNNRegressor(hidden_units=[10,10,10], feature_columns=feat_cols)
dnn_model.train(input_fn=train_input_func, steps=1000)

导致错误:

Traceback (most recent call last): File
"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1278, in _do_call
return fn(*args) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1263, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1350, in _call_tf_sessionrun
run_metadata) tensorflow.python.framework.errors_impl.InternalError: Unable to get
element as bytes.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:/Users/Admin/Documents/PycharmProjects/TF_Regression_Project/project.py",
line 69, in
dnn_model.train(input_fn=train_input_func, steps=1000) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 376, in train
loss = self._train_model(input_fn, hooks, saving_listeners) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 1145, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners) File
"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 1173, in _train_model_default
saving_listeners) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py",
line 1451, in _train_with_estimator_spec
_, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss]) File
"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py",
line 695, in exit
self._close_internal(exception_type) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py",
line 732, in _close_internal
self._sess.close() File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py",
line 980, in close
self._sess.close() File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py",
line 1124, in close
ignore_live_threads=True) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\coordinator.py",
line 389, in join
six.reraise(*self._exc_info_to_raise) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\six.py",
line 692, in reraise
raise value.with_traceback(tb) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\inputs\queues\feeding_queue_runner.py",
line 94, in _run
sess.run(enqueue_op, feed_dict=feed_dict) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 877, in run
run_metadata_ptr) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1100, in _run
feed_dict_tensor, options, run_metadata) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1272, in _do_run
run_metadata) File"C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py",
line 1291, in _do_call
raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InternalError: Unable to get
element as bytes.

里面出了什么问题?


问题在于规范化。

我没有使用sklearn方法,而是执行了以下操作:

1
2
3
df[['housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income',
    'median_house_value']] = df[['housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income',
               'median_house_value']].apply(lambda x: (x-x.min()) / (x.max()-x.min()))

因此,总而言之,我做了与sklearn相同的事情,但手动 - 使用lambda。