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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

454閲覧

UnboundLocalError: local variable 'ret' referenced before assignmentを解決したいです。

benzene0412

総合スコア3

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/03/02 07:33

実現したいこと

UnboundLocalError: local variable 'ret' referenced before assignmentのエラーを解決したいです。

10-20、44-49行目で変数retに対して代入が行われているはずですが、分かりません。

前提

DCLGAN実装中に以下のエラーメッセージが発生しました。

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

Traceback (most recent call last): File "train.py", line 14, in <module> model = create_model(opt) # create a model given opt.model and other options File "C:\Users\DCLGAN-main\models\__init__.py", line 65, in create_model instance = model(opt) File "C:\Users\DCLGAN-main\models\dcl_model.py", line 102, in __init__ self.criterionSim = torch.nn.L1Loss('sum').to(self.device) File "C:\Users\rt-pc\anaconda3\envs\DCLGAN\lib\site-packages\torch\nn\modules\loss.py", line 90, in __init__ super(L1Loss, self).__init__(size_average, reduce, reduction) File "C:\Users\rt-pc\anaconda3\envs\DCLGAN\lib\site-packages\torch\nn\modules\loss.py", line 17, in __init__ self.reduction = _Reduction.legacy_get_string(size_average, reduce) File "C:\Users\rt-pc\anaconda3\envs\DCLGAN\lib\site-packages\torch\nn\_reduction.py", line 44, in legacy_get_string warnings.warn(warning.format(ret)) UnboundLocalError: local variable 'ret' referenced before assignment

該当のソースコード

import warnings from typing import Optional # NB: Keep this file in sync with enums in aten/src/ATen/core/Reduction.h def get_enum(reduction): # type: (str) -> int if reduction == 'none': ret = 0 elif reduction == 'mean': ret = 1 elif reduction == 'elementwise_mean': warnings.warn("reduction='elementwise_mean' is deprecated, please use reduction='mean' instead.") ret = 1 elif reduction == 'sum': ret = 2 else: ret = -1 # TODO: remove once JIT exceptions support control flow raise ValueError("{} is not a valid value for reduction".format(reduction)) return ret # In order to support previous versions, accept boolean size_average and reduce # and convert them into the new constants for now # We use these functions in torch/legacy as well, in which case we'll silence the warning def legacy_get_string(size_average, reduce, emit_warning=True): # type: (Optional[bool], Optional[bool], bool) -> str warning = "size_average and reduce args will be deprecated, please use reduction='{}' instead." if size_average is None: size_average = True if reduce is None: reduce = True if size_average and reduce: reduce = 'mean' elif reduce: ret = 'sum' else: ret = 'none' if emit_warning: warnings.warn(warning.format(ret)) return ret def legacy_get_enum(size_average, reduce, emit_warning=True): # type: (Optional[bool], Optional[bool], bool) -> int return get_enum(legacy_get_string(size_average, reduce, emit_warning))

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

https://github.com/JunlinHan/DCLGAN

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

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

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

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

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

guest

回答1

0

ベストアンサー

提示コードはpytorch/torch/nn/_reduction.pyのコードと異なるので、pytorchのバージョンを最新に更新すると解消するかもしれません。

投稿2023/03/02 07:45

can110

総合スコア38266

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

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

benzene0412

2023/03/02 08:26

ありがとうございます。 pytorchのバージョンを更新したところ解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問