Я пытаюсь тренировать глубокую плотную нейронную сеть, используя тензор потока ядра.По сути, я адаптирую код, используемый в этом посте https://www.kaggle.com/mohitguptaomg/4-layer-dense-neural-net-using-tensorflow, для своего набора данных и моего собственного стиля кодирования.
Вот набор данных, который я использую: https://drive.google.com/open?id=1bDZVuiKyEDxUaY_mZgAKMIionicLs0yK
Основное различие с кодом в том, что я работаю, начиная с данных кадра данных, а не с массива,несмотря на это, я считаю, что я адаптировал это правильно.Я получаю ошибку:
Cannot feed value of shape (242,) for Tensor 'Placeholder_1:0', which has shape '(242, 1)'
Вот весь мой код: загрузка данных и импорт библиотеки
import pandas as pd
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import numpy as np
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
df= pd.read_csv('/home/nacho/Descargas/datasets/heart-disease-uci/heart.csv')
Назначение переменных:
X = df.drop('target', axis = 1)
Y = df["target"]
X,Y = shuffle (X, Y, random_state = 0)
train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size = 0.20, random_state = 0)
Теоретическая архитектура сети:
# The learning rate we want for our gradient descent, and the number of epochs
# We want to use to train our data
learning_rate = 0.2
training_epochs = 500
# The number of layers we want, with the number of neurons we want them
n_hidden_1 = 60
n_hidden_2 = 60
n_hidden_3 = 60
n_hidden_4 = 60
# Define cost function and training algorithm
costf = 'cross entropy'
traininga = "gradient descent optimizer"
Создание объектов, которые будут размещены в нейронной сети
# We define the inputs as placeholder, we shall fill them when we execute our code
n_dim = X.shape[1] # This will help define the vectors and matrices for calculation correctly
n_class = 1 # The number of categorie values possibles for Y
# We need to solve the nlen thing
x = tf.placeholder( tf.float32, [None, n_dim]) # Specifying where we are going to put the vectors
y_ = tf.placeholder(tf.float32, [None, n_class])
# We define out weights and bias as variables also
W = tf.Variable(tf.zeros([n_dim, n_class]))
b = tf.Variable(tf.zeros([n_class]))
weights = {
'h1': tf.Variable(tf.truncated_normal([n_dim, n_hidden_1])),
'h2': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])),
'h3': tf.Variable(tf.truncated_normal([n_hidden_2, n_hidden_3])),
'h4': tf.Variable(tf.truncated_normal([n_hidden_3, n_hidden_4])),
'out': tf.Variable(tf.truncated_normal([n_hidden_4, n_class]))
}
biases = {
'b1': tf.Variable(tf.truncated_normal([n_hidden_1])),
'b2': tf.Variable(tf.truncated_normal([n_hidden_2])),
'b3': tf.Variable(tf.truncated_normal([n_hidden_3])),
'b4': tf.Variable(tf.truncated_normal([n_hidden_4])),
'out': tf.Variable(tf.truncated_normal([n_class]))
}
Кодирование модели:
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activationsd
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with sigmoid activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Hidden layer with sigmoid activation
layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
layer_3 = tf.nn.relu(layer_3)
# Hidden layer with RELU activation
layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4'])
layer_4 = tf.nn.sigmoid(layer_4)
# Output layer with linear activation
out_layer = tf.matmul(layer_4, weights['out']) + biases['out']
return out_layer
# Calling model
y = multilayer_perceptron(x, weights, biases) # Basically, this will execute all our layers computations, resulting
# in a tensor y with our predicted results.
cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_)) # Calculates the cross_entropy
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
Кодирование дополнительных объектов, которые позволятчтобы потом получить дополнительные данные
# We are going to create lists, that will allow us to plot the evolution of the epochs accuracy and error after traini
mse_history = []
accuracy_history = []
Кодирование выполнения (обратите внимание, здесь происходит ошибка)
init = tf.global_variables_initializer()
# The session object we are going to need for execution
sess = tf.Session()
sess.run(init) # We initialize the global variables
for epoch in range(training_epochs):
sess.run(training_step, feed_dict = {x: train_x, y_: train_y}) # We start with the training
cost = sess.run(cost_function, feed_dict={x: train_x, y_: train_y}) #We calculate the loss for that epoch
cost_history = np.append(cost_history, cost) # With that loss calculted we append it to a list
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # We calculate what would be the correct prediction
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # We define a function to calculate accuracy
pred_y = sess.run(y, feed_dict = {x: test_x}) # Predict after training in the epoch
mse = tf.reduce_mean(tf.square(pred_y - test_y)) # define a function to Calculate the error of that epoch
mse_ = sess.run(mse) # we run said function
mse_history.append(mse_) # We append the result to a list
accuracy = (sess.run(accuracy, feed_dict={x: train_x, y_: train_y})) # Execute the accuracy function
accuracy_history.append(accuracy) # Append the result of the accuracy to a list
Ошибка, которую мы получаем:
ValueError Traceback (most recent call last)
<ipython-input-33-91216a39c8b4> in <module>
1 for epoch in range(training_epochs):
----> 2 sess.run(training_step, feed_dict = {x: train_x, y_: train_y}) # We start with the training
3 cost = sess.run(cost_function, feed_dict={x: train_x, y_: train_y}) #We calculate the loss for that epoch
4 cost_history = np.append(cost_history, cost) # With that loss calculted we append it to a list
5 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # We calculate what would be the correct prediction
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
928 try:
929 result = self._run(None, fetches, feed_dict, options_ptr,
--> 930 run_metadata_ptr)
931 if run_metadata:
932 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1127 'which has shape %r' %
1128 (np_val.shape, subfeed_t.name,
-> 1129 str(subfeed_t.get_shape())))
1130 if not self.graph.is_feedable(subfeed_t):
1131 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (242,) for Tensor 'Placeholder_1:0', which has shape '(242, 1)'
Я попытался заменить в разделе выполнения (последний перед ошибкой) все ссылки на наборы данных на .values, поэтому они будут передаваться как массивы.Например, вместо вызова X
, я вызываю X.values
, ошибка не исчезла.
Я новичок в tensorlow
, я знаю, что, возможно, смогу кодировать это проще с помощью API оценщика, но ядействительно хочу иметь возможность кодировать сети низкого уровня, чтобы убедиться, что я правильно их понимаю.При попытке указать точное количество строк данных при вызове заполнителей x
и y_
ошибка также сохранялась, но с небольшим разбросом точного сообщения.
Используемые версии:
jupyterlab 0.35.4 py36hf63ae98_0
jupyterlab_server 0.2.0 py36_0
keras-applications 1.0.7 pypi_0 pypi
keras-preprocessing 1.0.9
scikit-image 0.14.2 py36he6710b0_0
scikit-learn 0.20.3 py36hd81dba3_0
scipy 1.2.1 py36h7c811a0_0
tensorflow 2.0.0a0
anaconda 2019.03 py36_0
anaconda-client 1.7.2 py36_0
anaconda-project 0.8.2
Я буду продолжать работать с самого конца, я просто хочу понять тензор потока.
**** Редактировать день 2/05 /2019: ****
Таким образом, я изменил следующие строки и получаю интересные авансы:
x = tf.placeholder(tf.float32) # Specifying where we are going to put the vectors
y_ = tf.placeholder(tf.float32)
Просто изменив эти две строки в начале, изменил представленную ошибку наследующее:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-142-91216a39c8b4> in <module>
6 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # We define a function to calculate accuracy
7 pred_y = sess.run(y, feed_dict = {x: test_x}) # Predict after training in the epoch
----> 8 mse = tf.reduce_mean(tf.square(pred_y - test_y)) # define a function to Calculate the error of that epoch
9 mse_ = sess.run(mse) # we run said function
10 mse_history.append(mse_) # We append the result to a list
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(left, right)
1583 result = safe_na_op(lvalues, rvalues)
1584 return construct_result(left, result,
-> 1585 index=left.index, name=res_name, dtype=None)
1586
1587 wrapper.__name__ = op_name
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/pandas/core/ops.py in _construct_result(left, result, index, name, dtype)
1472 not be enough; we still need to override the name attribute.
1473 """
-> 1474 out = left._constructor(result, index=index, dtype=dtype)
1475
1476 out.name = name
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
260 else:
261 data = sanitize_array(data, index, dtype, copy,
--> 262 raise_cast_failure=True)
263
264 data = SingleBlockManager(data, index, fastpath=True)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/pandas/core/internals/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure)
656 elif subarr.ndim > 1:
657 if isinstance(data, np.ndarray):
--> 658 raise Exception('Data must be 1-dimensional')
659 else:
660 subarr = com.asarray_tuplesafe(data, dtype=dtype)
Exception: Data must be 1-dimensional
После этого я изменил код выполнения, добавив аргументы .value (размеры, казалось, были хорошими, должен был быть факт, что я использовал фрейм данных в качестве аргумента),из-за этого код выглядел следующим образом:
for epoch in range(training_epochs):
sess.run(training_step, feed_dict = {x: train_x.values, y_: train_y.values}) # We start with the training
cost = sess.run(cost_function, feed_dict={x: train_x.values, y_: train_y.values}) #We calculate the loss for that epoch
cost_history = np.append(cost_history, cost) # With that loss calculted we append it to a list
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # We calculate what would be the correct prediction
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # We define a function to calculate accuracy
pred_y = sess.run(y, feed_dict = {x: test_x.values}) # Predict after training in the epoch
mse = tf.reduce_mean(tf.square(pred_y - test_y.values)) # define a function to Calculate the error of that epoch
mse_ = sess.run(mse) # we run said function
mse_history.append(mse_) # We append the result to a list
accuracy = (sess.run(accuracy, feed_dict={x: train_x.values, y_: train_y.values})) # Execute the accuracy function
accuracy_history.append(accuracy)
это изменилось, снова ошибка к следующему:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1334 try:
-> 1335 return fn(*args)
1336 except errors.OpError as e:
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1319 return self._call_tf_sessionrun(
-> 1320 options, feed_dict, fetch_list, target_list, run_metadata)
1321
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1407 self._session, options, feed_dict, fetch_list, target_list,
-> 1408 run_metadata)
1409
InvalidArgumentError: Expected dimension in the range [-1, 1), but got 1
[[{{node ArgMax_1561}}]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-176-fc9234678b87> in <module>
9 mse_ = sess.run(mse) # we run said function
10 mse_history.append(mse_) # We append the result to a list
---> 11 accuracy = (sess.run(accuracy, feed_dict={x: train_x.values, y_: train_y.values})) # Execute the accuracy function
12 accuracy_history.append(accuracy)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
928 try:
929 result = self._run(None, fetches, feed_dict, options_ptr,
--> 930 run_metadata_ptr)
931 if run_metadata:
932 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1151 if final_fetches or final_targets or (handle and feed_dict_tensor):
1152 results = self._do_run(handle, final_targets, final_fetches,
-> 1153 feed_dict_tensor, options, run_metadata)
1154 else:
1155 results = []
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1327 if handle is None:
1328 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1329 run_metadata)
1330 else:
1331 return self._do_call(_prun_fn, handle, feeds, fetches)
~/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1347 pass
1348 message = error_interpolation.interpolate(message, self._graph)
-> 1349 raise type(e)(node_def, op, message)
1350
1351 def _extend_graph(self):
InvalidArgumentError: Expected dimension in the range [-1, 1), but got 1
[[node ArgMax_1561 (defined at <ipython-input-176-fc9234678b87>:5) ]]
Errors may have originated from an input operation.
Input Source operations connected to node ArgMax_1561:
Placeholder_19 (defined at <ipython-input-166-844432d3b8cf>:11)
Original stack trace for 'ArgMax_1561':
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 505, in start
self.io_loop.start()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 148, in start
self.asyncio_loop.run_forever()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/asyncio/base_events.py", line 438, in run_forever
self._run_once()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/asyncio/base_events.py", line 1451, in _run_once
handle._run()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/ioloop.py", line 690, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/ioloop.py", line 743, in _run_callback
ret = callback()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/gen.py", line 781, in inner
self.run()
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/gen.py", line 742, in run
yielded = self.gen.send(value)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 357, in process_one
yield gen.maybe_future(dispatch(*args))
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 267, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 534, in execute_request
user_expressions, allow_stdin,
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 294, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2848, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2874, in _run_cell
return runner(coro)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/async_helpers.py", line 67, in _pseudo_sync_runner
coro.send(None)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3049, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3214, in run_ast_nodes
if (yield from self.run_code(code, result)):
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-176-fc9234678b87>", line 5, in <module>
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # We calculate what would be the correct prediction
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 137, in argmax
return argmax_v2(input, axis, output_type, name)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 166, in argmax_v2
return gen_math_ops.arg_max(input, axis, name=name, output_type=output_type)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 938, in arg_max
name=name)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 800, in _apply_op_helper
op_def=op_def)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3479, in create_op
op_def=op_def)
File "/home/nacho/anaconda3/envs/deepl1/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1961, in __init__
self._traceback = tf_stack.extract_stack()
впоследствии я попытался удалить последние две строки, когда я читалгде-то, что они были частью подобной проблемы для кого-то другого
accuracy = (sess.run(accuracy, feed_dict={x: train_x.values, y_: train_y.values})) # Execute the accuracy function
accuracy_history.append(accuracy)
И код, кажется, работает.ТАК проблема должна быть где-то там.