質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

7262閲覧

ValueError: x and y must have same first dimension, but have shapes (100,) and (1,) の解決

ishikawa_a

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2021/08/27 00:19

前提・実現したいこと

活性化関数の学習中です。
演習問題をクリアーできません。
エラー解決方法をご教示ください。

発生している問題・エラーメッセージ

ValueError Traceback (most recent call last) <ipython-input-24-0ccc19cee4f9> in <module> 27 plt.plot(x_plt, y_relu, label='relu(x)') 28 y_relu_d = relu_deriv(x_plt) ---> 29 plt.plot(x_plt, y_relu_d, label='relu_deriv(x)') 30 plt.legend(loc='best') 31 plt.show() ~.conda\envs\tf\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs) 3017 @_copy_docstring_and_deprecators(Axes.plot) 3018 def plot(*args, scalex=True, scaley=True, data=None, **kwargs): -> 3019 return gca().plot( 3020 *args, scalex=scalex, scaley=scaley, 3021 **({"data": data} if data is not None else {}), **kwargs) ~.conda\envs\tf\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1603 """ 1604 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D) -> 1605 lines = [*self._get_lines(*args, data=data, **kwargs)] 1606 for line in lines: 1607 self.add_line(line) ~.conda\envs\tf\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data, *args, **kwargs) 313 this += args[0], 314 args = args[1:] --> 315 yield from self._plot_args(this, kwargs) 316 317 def get_next_color(self): ~.conda\envs\tf\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs, return_kwargs) 499 500 if x.shape[0] != y.shape[0]: --> 501 raise ValueError(f"x and y must have same first dimension, but " 502 f"have shapes {x.shape} and {y.shape}") 503 if x.ndim > 2 or y.ndim > 2: ValueError: x and y must have same first dimension, but have shapes (100,) and (1,)

該当のソースコード

python

1import matplotlib.pyplot as plt 2import numpy as np 3 4# matplotlib inline 5 6 7def relu(x): 8 #--------------- 9 if x.any() < 0: 10 return 0 11 else: 12 return x 13 #--------------- 14 15 16def relu_deriv(x): 17 #--------------- 18 if x.any() < 0: 19 return 0 20 else: 21 return 1 22 #--------------- 23 24 25x_plt = np.arange(-5, 5, 0.1) 26y_relu = relu(x_plt) 27plt.plot(x_plt, y_relu, label='relu(x)') 28y_relu_d = relu_deriv(x_plt) 29plt.plot(x_plt, y_relu_d, label='relu_deriv(x)') 30plt.legend(loc='best') 31plt.show()

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

import matplotlib.pyplot as plt
import numpy as np

matplotlib inline

def relu(x):
#---------------
return np.where(x > 0, x, 0)
#---------------

def relu_deriv(x):
#---------------
return np.where(x > 0, 1, 0)
#---------------

x_plt = np.arange(-5, 5, 0.1)
y_relu_d = relu_deriv(x_plt)
plt.plot(x_plt, y_relu_d, label='relu_deriv(x)')
y_relu = relu(x_plt)
plt.plot(x_plt, y_relu, label='relu(x)')
plt.legend(loc='best')
plt.show()

投稿2021/08/31 05:04

ishikawa_a

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

コードの最後に下記を追加して、その結果を見て考えてみてください

python

1print(x_plt.shape) 2print(y_relu_d)

投稿2021/08/27 00:40

jbpb0

総合スコア7651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ishikawa_a

2021/08/31 05:04

早速のご返答ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問