<commit_before>
// Copyright 2020 The SwiftFusion Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TensorFlow
import PenguinStructures

/// A class performing common activities in the RandomProjection framework.
/// The decomposition used here is in:
/// <NAME> and <NAME>, Probabilistic Principal Component Analysis,
/// Journal of the Royal Statistical Society. Series B, Vol. 61, No. 3 (1999), pp. 611-622
/// Input shape for training: [N, H, W, C]
/// W matrix: [H, W, C, latent]
/// Output: [H, W, C]
public struct RandomProjection {
  public typealias Patch = Tensor<Double>

  /// Random Basis Matrix
  /// When input image is of shape [N, H, W, C]
  /// B is of shape [d, H * W * C]
  public let B: Tensor<Double>

  /// Initialize the random projector with a normalized projection matrix
  public init(fromShape shape: TensorShape, toFeatureSize d: Int) {
    let (H, W, C) = (shape[0], shape[1], shape[2])
    B = Tensor<Double>(
      stacking: (0..<d).map { _ in
        let t = Tensor<Double>(randomNormal: [H * W * C])
        return t/sqrt(t.squared().sum())
      }
    )
  }

  /// Generate an image according to a latent
  /// Input: [H, W, C]
  /// Output: [d]
  @differentiable
  public func encode(_ image: Patch) -> Tensor<Double> {
    precondition(image.rank == 3 || (image.rank == 4), "wrong latent dimension \(image.shape)")
    let HWC = B.shape[1]
    let d = B.shape[0]
    if image.rank == 4 {
      let N = image.shape[0]
      let v_T = (image).reshaped(to: [HWC, image.shape[0]]).transposed()
      return matmul(v_T, B.transposed()).reshaped(to: [image.shape[0], d])
    } else {
      return matmul(B, (image).reshaped(to: [HWC, 1])).reshaped(to: [d])
    }
  }
}

extension RandomProjection: AppearanceModelEncoder {}
<commit_msg>Fix comment for random projections<commit_after>
// Copyright 2020 The SwiftFusion Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import TensorFlow
import PenguinStructures

/// A class performing common activities in the RandomProjection framework.
/// Input shape for training: [N, H, W, C]
/// W matrix: [feature, H*W*C]
/// Output: [feature]
public struct RandomProjection {
  public typealias Patch = Tensor<Double>

  /// Random Basis Matrix
  /// When input image is of shape [N, H, W, C]
  /// B is of shape [d, H * W * C]
  public let B: Tensor<Double>

  /// Initialize the random projector with a normalized projection matrix
  public init(fromShape shape: TensorShape, toFeatureSize d: Int) {
    let (H, W, C) = (shape[0], shape[1], shape[2])
    B = Tensor<Double>(
      stacking: (0..<d).map { _ in
        let t = Tensor<Double>(randomNormal: [H * W * C])
        return t/sqrt(t.squared().sum())
      }
    )
  }

  /// Generate an feature from image
  /// Input: [H, W, C]
  /// Output: [d]
  @differentiable
  public func encode(_ image: Patch) -> Tensor<Double> {
    precondition(image.rank == 3 || (image.rank == 4), "wrong feature dimension \(image.shape)")
    let HWC = B.shape[1]
    let d = B.shape[0]
    if image.rank == 4 {
      let N = image.shape[0]
      let v_T = (image).reshaped(to: [HWC, image.shape[0]]).transposed()
      return matmul(v_T, B.transposed()).reshaped(to: [image.shape[0], d])
    } else {
      return matmul(B, (image).reshaped(to: [HWC, 1])).reshaped(to: [d])
    }
  }
}

extension RandomProjection: AppearanceModelEncoder {}

{'fraction_non_alphanumeric': 0.13516557783284525, 'fraction_numerical': 0.012390177968010813, 'mean_word_length': 3.065934065934066, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081082', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1338, 'n_words': 518}