python和Matlab中返回函数的区别

时间:2017-11-09 01:58:36

标签: python matlab

我正在学习python语言,看起来python和Matlab中的return命令之间存在一些差异。在python中是否存在MATLAB返回函数的精确等价?

1 个答案:

答案 0 :(得分:1)

在Python函数与MATLAB函数中使用return关键字是不同的。

在MATLAB函数中,左手侧(LHS)参数定义了在返回之前需要在函数体中定义的内容。

import tensorflow as tf

batch_size = 128
time_steps = 50
char_size = 50

num_units = 256

sess = tf.InteractiveSession()

X = tf.placeholder(tf.float32, [batch_size, time_steps, char_size])

cell = tf.contrib.rnn.BasicLSTMCell(num_units)
cell = tf.contrib.rnn.MultiRNNCell([cell] * 2, state_is_tuple=True)

output, state = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)

如果使用return语句提前结束函数的执行,则仍然负责正确填充函数的LHS参数列表

Traceback (most recent call last):
  File ".\issue.py", line 16, in <module>
    output, state = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 598, in dynamic_rnn
    dtype=dtype)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 761, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2775, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2604, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2554, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 746, in _time_step
    (output, new_state) = call_cell()
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 732, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\base.py", line 450, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 938, in call
    cur_inp, new_state = cell(cur_inp, cur_state)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\layers\base.py", line 450, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 401, in call
    concat = _linear([inputs, h], 4 * self._num_units, True)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1039, in _linear
    initializer=kernel_initializer)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 360, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1405, in wrapped_custom_getter
    *args, **kwargs)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in _rnn_get_variable
    variable = getter(*args, **kwargs)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in _rnn_get_variable
    variable = getter(*args, **kwargs)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "C:\Users\uidq6096\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 669, in _get_single_variable
    found_var.get_shape()))
ValueError: Trying to share variable rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel, but specified shape (512, 1024) and found shape (306, 1024).

在这个例子中,foo返回1为y,因为函数体将y定义为1,然后在return语句之后返回给调用者。

在python中,return语句结束执行并且还定义了函数返回的值(如果有的话),python函数定义中没有输出参数列表。

function y = foo(x)
    y = 1;
 end

因为需要在python中使用return关键字来返回函数中的值,所以返回往往是比MATLAB函数定义中的返回更常用的构造。