q1_classifier.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:cs224d 作者: kkihara 项目源码 文件源码
def add_model(self, input_data):
    """Adds a linear-layer plus a softmax transformation

    The core transformation for this model which transforms a batch of input
    data into a batch of predictions. In this case, the mathematical
    transformation effected is

    y = softmax(xW + b)

    Hint: Make sure to create tf.Variables as needed. Also, make sure to use
          tf.name_scope to ensure that your name spaces are clean.
    Hint: For this simple use-case, it's sufficient to initialize both weights W
          and biases b with zeros.

    Args:
      input_data: A tensor of shape (batch_size, n_features).
    Returns:
      out: A tensor of shape (batch_size, n_classes)
    """
    ### YOUR CODE HERE
    with tf.name_scope('linear_layer'):
        W = tf.Variable(np.zeros((self.config.n_features,
                                  self.config.n_classes)).astype(np.float32))
        b = tf.Variable(np.zeros((self.config.n_classes,)).astype(np.float32))
        out = softmax(tf.matmul(input_data, W) + b)
    ### END YOUR CODE
    return out
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号