您必须为dtype double且形状为[25,25]的占位符张量'Placeholder_2'提供一个值>

问题描述 投票:1回答:1

我正在尝试构建自己的ELM,我需要获取在python中的类对象中初始化的Beta权重。但是,我尝试了几乎可以在张量指南或互联网上找到的所有东西,但是我认为这是构造代码的愚蠢错误。

我的代码看起来像这样

def __init__(self,input_nodes,hidden_nodes,output_nodes):
 ....
  self.__beta = tf.zeros((Hidden_nodes,Output_nodes),dtype=tf.dtypes.float64,name=None)

def seq_train_graph(self):
   ....
  UPDATE = tf.matmul(tf.matmul(K_inverse, HT), inverse_acti_y - tf.matmul(H, self.__beta))
  An = self.__beta + UPDATE
  self.__beta = An

def retrieve_beta(self):
    return self.__sess.run(self.__beta )

根据此类代码。函数retrieve_beta用于获取网络的beta权重的值。运行代码时,出现以下错误

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype double and shape [25,25]
     [[node Placeholder_2 (defined at <ipython-input-17-8874f24bd69c>:18) ]]

我尝试通过添加feed_dict来喂养网络电话>

self.__sess.run(self.__beta ,feed_dict={An: self.An })

但没有运气。出现

的错误
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was Tensor("add_248:0", shape=(25, 100), dtype=float64) which was passed to the feed with key Tensor("Placeholder_5:0", shape=(25, 100), dtype=float64).

我相信应该得到一个nd数组而不是张量对象。但就我而言,据我所知,β权重仅由An(self .__ beta = An)的值初始化。或者如果我缺少什么,请帮助我。

谢谢

我正在尝试构建自己的ELM,我需要获取在python中的类对象中初始化的Beta权重。但是,我尝试了am =几乎可以在张量指南上找到的所有东西,或者从...

python tensorflow machine-learning
1个回答
0
投票

您需要使用tf.Variable而不是tf.placeholder初始化Beta。以下代码为您提供了ELM的自定义实现的详细信息。

import tensorflow as tf
import numpy as np

# CHECK : Constants
omega = 1.

class ELM(object):
  def __init__(self, sess, batch_size, input_len, hidden_num, output_len):
    '''
    Args:
      sess : TensorFlow session.
      batch_size : The batch size (N)
      input_len : The length of input. (L)
      hidden_num : The number of hidden node. (K)
      output_len : The length of output. (O)
    '''

    self._sess = sess 
    self._batch_size = batch_size
    self._input_len = input_len
    self._hidden_num = hidden_num
    self._output_len = output_len 

    # for train
    self._x0 = tf.placeholder(tf.float32, [self._batch_size, self._input_len])
    self._t0 = tf.placeholder(tf.float32, [self._batch_size, self._output_len])

    # for test
    self._x1 = tf.placeholder(tf.float32, [None, self._input_len])
    self._t1 = tf.placeholder(tf.float32, [None, self._output_len])

    self._W = tf.Variable(
      tf.random_normal([self._input_len, self._hidden_num]),
      trainable=False, dtype=tf.float32)
    self._b = tf.Variable(
      tf.random_normal([self._hidden_num]),
      trainable=False, dtype=tf.float32)
    self._beta = tf.Variable(
      tf.zeros([self._hidden_num, self._output_len]),
      trainable=False, dtype=tf.float32)
    self._var_list = [self._W, self._b, self._beta]

    self.H0 = tf.matmul(self._x0, self._W) + self._b # N x L
    self.H0_T = tf.transpose(self.H0)

    self.H1 = tf.matmul(self._x1, self._W) + self._b # N x L
    self.H1_T = tf.transpose(self.H1)

    # beta analytic solution : self._beta_s (K x O)
    if self._input_len < self._hidden_num: # L < K
      identity = tf.constant(np.identity(self._hidden_num), dtype=tf.float32)
      self._beta_s = tf.matmul(tf.matmul(tf.matrix_inverse(
        tf.matmul(self.H0_T, self.H0) + identity/omega), 
        self.H0_T), self._t0)
      # _beta_s = (H_T*H + I/om)^(-1)*H_T*T
    else:
      identity = tf.constant(np.identity(self._batch_size), dtype=tf.float32)
      self._beta_s = tf.matmul(tf.matmul(self.H0_T, tf.matrix_inverse(
        tf.matmul(self.H0, self.H0_T)+identity/omega)), self._t0)
      # _beta_s = H_T*(H*H_T + I/om)^(-1)*T

    self._assign_beta = self._beta.assign(self._beta_s)
    self._fx0 = tf.matmul(self.H0, self._beta)
    self._fx1 = tf.matmul(self.H1, self._beta)

    self._cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = self._fx0, labels=self._t0))

    self._init = False
    self._feed = False

    # for the mnist test
    self._correct_prediction = tf.equal(tf.argmax(self._fx1,1), tf.argmax(self._t1,1))
    self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, tf.float32))

  def feed(self, x, t):
    '''
    Args :
      x : input array (N x L)
      t : label array (N x O)
    '''

    if not self._init : self.init()
    self._sess.run(self._assign_beta, {self._x0:x, self._t0:t})
    self._feed = True

  def init(self):
    self._sess.run(tf.initialize_variables(self._var_list))
    self._init = True

  def test(self, x, t=None):
    if not self._feed : exit("Not feed-forward trained")
    if t is not None :
      print("Accuracy: {:.9f}".format(self._sess.run(self._accuracy, {self._x1:x, self._t1:t})))
    else :
      return self._sess.run(self._fx1, {self._x1:x})  
热门问题
推荐问题
最新问题