/// <summary> /// Save feature weights into file /// </summary> /// <param name="filename"></param> /// <param name="bVQ"></param> /// <returns></returns> public void SaveFeatureWeight(string filename, bool bVQ) { var filename_alpha = filename + ".alpha"; var tofs = new StreamWriter(filename_alpha, false); var bw = new BinaryWriter(tofs.BaseStream); if (bVQ == true) { Logger.WriteLine("Save feature weights into a VQ model: {0}", filename_alpha); //Build code book VectorQuantization vq = new VectorQuantization(); for (long i = 1; i <= maxid_; i++) { vq.Add(alpha_[i]); } int vqSize = 256; double distortion = vq.BuildCodebook(vqSize); Logger.WriteLine("Weight vector quantization distortion: {0}", distortion); //VQ size bw.Write(vqSize); //Save VQ codebook into file for (int j = 0; j < vqSize; j++) { bw.Write(vq.CodeBook[j]); } //Save weights for (long i = 1; i <= maxid_; ++i) { bw.Write((byte)vq.ComputeVQ(alpha_[i])); } } else { Logger.WriteLine("Save feature weights into a normal model: {0}", filename_alpha); bw.Write(0); //Save weights for (long i = 1; i <= maxid_; ++i) { bw.Write((float)alpha_[i]); } } bw.Close(); }
private void SaveLSTMWeights(Vector4[][] weight, BinaryWriter fo, bool bVQ = false) { var w = weight.Length; var h = weight[0].Length; var vqSize = 256; Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vq:{2}", w, h, bVQ); fo.Write(weight.Length); fo.Write(weight[0].Length); if (bVQ == false) { fo.Write(0); for (var i = 0; i < w; i++) { for (var j = 0; j < h; j++) { fo.Write(weight[i][j].X); fo.Write(weight[i][j].Y); fo.Write(weight[i][j].Z); fo.Write(weight[i][j].W); } } } else { //Build vector quantization model var vqInputCell = new VectorQuantization(); var vqInputForgetGate = new VectorQuantization(); var vqInputInputGate = new VectorQuantization(); var vqInputOutputGate = new VectorQuantization(); for (var i = 0; i < w; i++) { for (var j = 0; j < h; j++) { vqInputInputGate.Add(weight[i][j].X); vqInputForgetGate.Add(weight[i][j].Y); vqInputCell.Add(weight[i][j].Z); vqInputOutputGate.Add(weight[i][j].W); } } var distortion = vqInputInputGate.BuildCodebook(vqSize); Logger.WriteLine("InputInputGate distortion: {0}", distortion); distortion = vqInputForgetGate.BuildCodebook(vqSize); Logger.WriteLine("InputForgetGate distortion: {0}", distortion); distortion = vqInputCell.BuildCodebook(vqSize); Logger.WriteLine("InputCell distortion: {0}", distortion); distortion = vqInputOutputGate.BuildCodebook(vqSize); Logger.WriteLine("InputOutputGate distortion: {0}", distortion); fo.Write(vqSize); //Save InputInputGate VQ codebook into file for (var j = 0; j < vqSize; j++) { fo.Write(vqInputInputGate.CodeBook[j]); } //Save InputForgetGate VQ codebook into file for (var j = 0; j < vqSize; j++) { fo.Write(vqInputForgetGate.CodeBook[j]); } //Save InputCell VQ codebook into file for (var j = 0; j < vqSize; j++) { fo.Write(vqInputCell.CodeBook[j]); } //Save InputOutputGate VQ codebook into file for (var j = 0; j < vqSize; j++) { fo.Write(vqInputOutputGate.CodeBook[j]); } for (var i = 0; i < w; i++) { for (var j = 0; j < h; j++) { fo.Write((byte)vqInputInputGate.ComputeVQ(weight[i][j].X)); fo.Write((byte)vqInputForgetGate.ComputeVQ(weight[i][j].Y)); fo.Write((byte)vqInputCell.ComputeVQ(weight[i][j].Z)); fo.Write((byte)vqInputOutputGate.ComputeVQ(weight[i][j].W)); } } } }
//Save matrix into file as binary format public static void SaveMatrix(Matrix <float> mat, BinaryWriter fo, bool bVQ = false) { //Save the width and height of the matrix fo.Write(mat.Width); fo.Write(mat.Height); if (bVQ == false) { Logger.WriteLine("Saving matrix without VQ..."); fo.Write(0); // non-VQ //Save the data in matrix for (var r = 0; r < mat.Height; r++) { for (var c = 0; c < mat.Width; c++) { fo.Write(mat[r][c]); } } } else { //Build vector quantization matrix var vqSize = 256; var vq = new VectorQuantization(); Logger.WriteLine("Saving matrix with VQ {0}...", vqSize); var valSize = 0; for (var i = 0; i < mat.Height; i++) { for (var j = 0; j < mat.Width; j++) { vq.Add(mat[i][j]); valSize++; } } if (vqSize > valSize) { vqSize = valSize; } var distortion = vq.BuildCodebook(vqSize); Logger.WriteLine("Distortion: {0}, vqSize: {1}", distortion, vqSize); //Save VQ codebook into file fo.Write(vqSize); for (var j = 0; j < vqSize; j++) { fo.Write(vq.CodeBook[j]); } //Save the data in matrix for (var r = 0; r < mat.Height; r++) { for (var c = 0; c < mat.Width; c++) { fo.Write((byte)vq.ComputeVQ(mat[r][c])); } } } }
public bool BuildVQModel(string strFileName) { int vqSize = 256; if (entireTermList == null || entireTermList.Count == 0) { return(false); } StreamWriter fo = new StreamWriter(strFileName); BinaryWriter bw = new BinaryWriter(fo.BaseStream); // Save the word vectors bw.Write(entireTermList.Count); //Vocabulary size bw.Write(vectorSize); //Vector size bw.Write(vqSize); //VQ size Logger.WriteLine("vocabulary size: {0}, vector size: {1}, vq size: {2}", entireTermList.Count, vectorSize, vqSize); //Create word and VQ values mapping table Dictionary <string, List <byte> > vqResult = new Dictionary <string, List <byte> >(); foreach (Term term in entireTermList) { vqResult.Add(term.strTerm, new List <byte>()); } Logger.WriteLine("Dims Distortion:"); for (int i = 0; i < vectorSize; i++) { //Generate VQ values for each dimension VectorQuantization vq = new VectorQuantization(); for (int j = 0; j < entireTermList.Count; j++) { vq.Add(entireTermList[j].vector[i]); } double distortion = vq.BuildCodebook(vqSize); Logger.WriteLine("Dim {0}: {1}", i, distortion); for (int j = 0; j < entireTermList.Count; j++) { byte vqValue = (byte)vq.ComputeVQ(entireTermList[j].vector[i]); vqResult[entireTermList[j].strTerm].Add(vqValue); } //Save VQ codebook into model file for (int j = 0; j < vqSize; j++) { bw.Write(vq.CodeBook[j]); } } foreach (KeyValuePair <string, List <byte> > pair in vqResult) { if (pair.Value.Count != vectorSize) { throw new Exception(String.Format("word {0} has inconsistent vector size: orginial size is {1}, vq size is {2}", pair.Key, vectorSize, pair.Value.Count)); } //term string bw.Write(pair.Key); //term vector for (int b = 0; b < pair.Value.Count; b++) { bw.Write(pair.Value[b]); } } bw.Flush(); fo.Flush(); fo.Close(); return(true); }
//Save matrix into file as binary format public static void SaveMatrix(Matrix<double> mat, BinaryWriter fo, bool bVQ = false) { //Save the width and height of the matrix fo.Write(mat.Width); fo.Write(mat.Height); if (bVQ == false) { Logger.WriteLine("Saving matrix without VQ..."); fo.Write(0); // non-VQ //Save the data in matrix for (int r = 0; r < mat.Height; r++) { for (int c = 0; c < mat.Width; c++) { fo.Write(mat[r][c]); } } } else { //Build vector quantization matrix int vqSize = 256; VectorQuantization vq = new VectorQuantization(); Logger.WriteLine("Saving matrix with VQ {0}...", vqSize); int valSize = 0; for (int i = 0; i < mat.Height; i++) { for (int j = 0; j < mat.Width; j++) { vq.Add(mat[i][j]); valSize++; } } if (vqSize > valSize) { vqSize = valSize; } double distortion = vq.BuildCodebook(vqSize); Logger.WriteLine("Distortion: {0}, vqSize: {1}", distortion, vqSize); //Save VQ codebook into file fo.Write(vqSize); for (int j = 0; j < vqSize; j++) { fo.Write(vq.CodeBook[j]); } //Save the data in matrix for (int r = 0; r < mat.Height; r++) { for (int c = 0; c < mat.Width; c++) { fo.Write((byte)vq.ComputeVQ(mat[r][c])); } } } }
private void saveLSTMWeight(Vector4[][] weight, BinaryWriter fo, bool bVQ = false) { int w = weight.Length; int h = weight[0].Length; int vqSize = 256; Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vq:{2}", w, h, bVQ); fo.Write(weight.Length); fo.Write(weight[0].Length); if (bVQ == false) { fo.Write(0); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { fo.Write(weight[i][j].X); fo.Write(weight[i][j].Y); fo.Write(weight[i][j].Z); fo.Write(weight[i][j].W); } } } else { //Build vector quantization model VectorQuantization vqInputCell = new VectorQuantization(); VectorQuantization vqInputForgetGate = new VectorQuantization(); VectorQuantization vqInputInputGate = new VectorQuantization(); VectorQuantization vqInputOutputGate = new VectorQuantization(); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { vqInputInputGate.Add(weight[i][j].X); vqInputForgetGate.Add(weight[i][j].Y); vqInputCell.Add(weight[i][j].Z); vqInputOutputGate.Add(weight[i][j].W); } } double distortion = 0.0; distortion = vqInputInputGate.BuildCodebook(vqSize); Logger.WriteLine("InputInputGate distortion: {0}", distortion); distortion = vqInputForgetGate.BuildCodebook(vqSize); Logger.WriteLine("InputForgetGate distortion: {0}", distortion); distortion = vqInputCell.BuildCodebook(vqSize); Logger.WriteLine("InputCell distortion: {0}", distortion); distortion = vqInputOutputGate.BuildCodebook(vqSize); Logger.WriteLine("InputOutputGate distortion: {0}", distortion); fo.Write(vqSize); //Save InputInputGate VQ codebook into file for (int j = 0; j < vqSize; j++) { fo.Write(vqInputInputGate.CodeBook[j]); } //Save InputForgetGate VQ codebook into file for (int j = 0; j < vqSize; j++) { fo.Write(vqInputForgetGate.CodeBook[j]); } //Save InputCell VQ codebook into file for (int j = 0; j < vqSize; j++) { fo.Write(vqInputCell.CodeBook[j]); } //Save InputOutputGate VQ codebook into file for (int j = 0; j < vqSize; j++) { fo.Write(vqInputOutputGate.CodeBook[j]); } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { fo.Write((byte)vqInputInputGate.ComputeVQ(weight[i][j].X)); fo.Write((byte)vqInputForgetGate.ComputeVQ(weight[i][j].Y)); fo.Write((byte)vqInputCell.ComputeVQ(weight[i][j].Z)); fo.Write((byte)vqInputOutputGate.ComputeVQ(weight[i][j].W)); } } } }
private void saveLSTMWeight(LSTMWeight[][] weight, BinaryWriter fo) { int w = weight.Length; int h = weight[0].Length; int vqSize = 256; Logger.WriteLine("Saving LSTM weight matrix. width:{0}, height:{1}, vqSize:{2}", w, h, vqSize); fo.Write(weight.Length); fo.Write(weight[0].Length); //Build vector quantization model VectorQuantization vq = new VectorQuantization(); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { vq.Add(weight[i][j].wInputCell); vq.Add(weight[i][j].wInputForgetGate); vq.Add(weight[i][j].wInputInputGate); vq.Add(weight[i][j].wInputOutputGate); } } double distortion = vq.BuildCodebook(vqSize); Logger.WriteLine("Distortion: {0}", distortion); //Save VQ codebook into file fo.Write(vqSize); for (int j = 0; j < vqSize; j++) { fo.Write(vq.CodeBook[j]); } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputCell)); fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputForgetGate)); fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputInputGate)); fo.Write((byte)vq.ComputeVQ(weight[i][j].wInputOutputGate)); } } }