파이썬 에러관련

wjdgus8576의 이미지

!pip install tensorflow==1.12.0
import tensorflow as tf
import numpy as np
print(tf.__version__)
print(np.__version__)
 
data=np.array([[1,2,3,4],[1,1,4,0],[-1,-2,-4,2]],dtype=np.float32) #3X4
print(data)
data_x=data[:,0:3] #3x3
data_y=data[:,3:] #3x1
sess=tf.Session()
print('\n')
 
print(data_x)
 
print('\n')
print(data_y)
 
 
data_p=tf.one_hot(tf.cast(data_y,dtype=tf.int32),depth=5) #3X5
data_p=tf.reshape(data_p,shape=[-1,5])
sess=tf.Session()
print('\n')
print(sess.run(data_p))
 
 
xp=tf.placeholder(tf.float32,shape=[None,3]) #3x3
#yp=tf.placeholder(tf.float32,shape=[None,1])
wv= tf.Variable(tf.random_normal([3,1])) #3x1
 
print('\n')
 
h1=tf.matmul(xp,wv)
print(h1)
loss1= tf.nn.softmax_cross_entropy_with_logits(labels=data_p,logits=h1)
loss1= tf.reduce_mean(loss1)
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss1)
sess.run(tf.global_variables_initializer())
 
 
print(sess.run(wv))
print(sess.run(train,feed_dict={xp:data_x}))
for i in range (2):
 
  print(sess.run(h1,feed_dict={xp:data_x}))
  print(sess.run(loss1,feed_dict={xp:data_x}))
 
문제 발생은 print(sess.run(train,feed_dict={xp:data_x}))에 일어납니다.
 feed_dict에 data_x값만 필요하다고 생각해서 주었으며, 아래에 오류중에 너무 길어 일부만 올리겠습니다
InvalidArgumentError: Input to reshape is a tensor with 15 values, but the requested shape has 3
	 [[{{node gradients/softmax_cross_entropy_with_logits_sg/Reshape_grad/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](gradients/softmax_cross_entropy_with_logits_sg_grad/mul, softmax_cross_entropy_with_logits_sg/Shape_1)]]
 
During handling of the above exception, another exception occurred:
 
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1346           pass
   1347       message = error_interpolation.interpolate(message, self._graph)
-> 1348       raise type(e)(node_def, op, message)
   1349 
   1350   def _extend_graph(self):
 
InvalidArgumentError: Input to reshape is a tensor with 15 values, but the requested shape has 3
	 [[node gradients/softmax_cross_entropy_with_logits_sg/Reshape_grad/Reshape (defined at <ipython-input-2-baa84e09ed6d>:31)  = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](gradients/softmax_cross_entropy_with_logits_sg_grad/mul, softmax_cross_entropy_with_logits_sg/Shape_1)]]
 
Caused by op 'gradients/softmax_cross_entropy_with_logits_sg/Reshape_grad/Reshape', defined at:
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 664, in launch_instance
다음과 같은 오류가 발생합니다.
도움 주시면 정말 감사하겠습니다 파일첨부하려고 하였으나, 파일형식문제로 첨부 못 하였습니다.
File attachments: 
첨부파일 크기
Image icon 제목 없음.png92 KB
입체김밥의 이미지

loss1= tf.nn.softmax_cross_entropy_with_logits(labels=data_p,logits=h1)

data_p와 h1의 마지막 차원의 크기가 달라서 생기는 오류입니다.

wjdgus8576의 이미지

해결했습니다.. 정말 감사합니다!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.