我想呼叫一個方法,該方法在另一個方法中回傳一組輸入,并使用我網路的當前權重進行預測。為簡單起見,我現在嘗試只列印輸入。
import tensorflow as tf
import numpy as np
inputs = tf.keras.layers.Input( shape=(10,) )
x= tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(2)(inputs)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.compile(loss = "mse",
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01) )
假設我有一個回傳numpy陣列的方法。
def firstMethod():
return np.array([[1.32040024, -1.11483181, 1.01526141, 1.36170304, -0.872175455, 1.23767245, 0.696531296, 1.74229145, -1.10529709, -3.96802974]])
現在,我定義了另一種方法,它將我的模型作為引數并列印陣列。
def secondMethod(model):
tf.print(tf.convert_to_tensor(firstMethod, dtype = tf.float32))
secondMethod(model)
我收到一個錯誤,想知道如何解決這個問題。
ValueError: Attempt to convert a value (<function firstMethod at 0x0000019E0C44B4C0>) with an unsupported type (<class 'function'>) to a Tensor.
uj5u.com熱心網友回復:
您沒有呼叫firstMethod(),而是將函式作為引數傳入。添加括號以呼叫該函式,它應該可以作業。此外,secondMethod()實際上并不使用model. 也許你打算做這樣的事情?
def secondMethod(model):
tf.print(tf.convert_to_tensor(model(firstMethod()), dtype = tf.float32))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375436.html
上一篇:AttributeError:'Functional'物件沒有屬性'predict_segmentation'匯入TensorFlow模型Keras時
